-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
37 lines (26 loc) · 945 Bytes
/
stack.py
File metadata and controls
37 lines (26 loc) · 945 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from typing import Generator, Any, Sequence
class Stack:
contents: list[str]
def __init__(self) -> None:
self.contents: list[str] = []
def push(self, val: str) -> None:
self.contents.append(val)
def push_multi(self, *items: str) -> None:
for item in items:
self.push(item)
def top(self) -> str:
if self.empty():
raise IndexError("pop from an empty stack")
return self.contents[-1]
def pop(self) -> str:
if self.empty():
raise IndexError("pop from an empty stack")
return self.contents.pop()
def pop_multi(self, count: int) -> Generator[Any, str, None]:
return (self.pop() for _ in range(count))
def depth(self) -> int:
return len(self.contents)
def empty(self) -> bool:
return len(self.contents) == 0
def __repr__(self) -> str:
return f"Stack(contents={self.contents})"