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()
True
>>> s.push(hello)
>>> s.is_empty()
False
return self._items == []
def push(self, item: Any) -> None:
Add a new element to the top of this stack.
self._items.append(item)
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()
goodbye
if self.is_empty():
raise EmptyStackError
else:
return self._items.pop()
class EmptyStackError(Exception):
Exception raised when an error occurs.
pass
Reviews
There are no reviews yet.