forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
45 lines (28 loc) · 799 Bytes
/
stack.py
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
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
__author__ = "Michael Ciccotosto-Camp"
from typing import TypeVar, Generic
T = TypeVar("T")
class Stack(Generic[T]):
def __init__(self) -> None:
self.__list: list[T] = []
def pop(self) -> T:
return self.__list.pop()
def push(self, element: T) -> int:
self.__list.append(element)
return len(self)
def top(self) -> T:
return self.__list[-1]
def __len__(self) -> int:
return len(self.__list)
def __str__(self) -> str:
return str(self.__list)
def main() -> None:
int_stack: Stack[int] = Stack()
int_stack.push(4)
int_stack.push(5)
int_stack.push(9)
print(int_stack.pop())
print(len(int_stack))
print(int_stack.top())
if __name__ == "__main__":
main()