CS代考计算机代写 from typing import Any, List

from typing import Any, List

class Stack:
“””A last-in-first-out (LIFO) stack of items.

Stores data in a last-in, first-out order. When removing an item from the
stack, the most recently-added item is the one that is removed.
# === Private Attributes ===
# _items:
# The items stored in this stack. The end of the list represents
# the top of the stack.
_items: List

def __init__(self) -> None:
“””Initialize a new empty stack.”””
self._items = []

def is_empty(self) -> bool:
“””Return whether this stack contains no items.

>>> s = Stack()
>>> s.is_empty()
>>> s.push(‘hello’)
>>> s.is_empty()
return self._items == []

def push(self, item: Any) -> None:
“””Add a new element to the top of this stack.”””

def pop(self) -> Any:
“””Remove and return the element at the top of this stack.

Raise an EmptyStackError if this stack is empty.

>>> s = Stack()
>>> s.push(‘hello’)
>>> s.push(‘goodbye’)
>>> s.pop()
if self.is_empty():
raise EmptyStackError
return self._items.pop()

class EmptyStackError(Exception):
“””Exception raised when an error occurs.”””

Leave a Reply

Your email address will not be published. Required fields are marked *