-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_stack.py
More file actions
47 lines (37 loc) · 1.02 KB
/
Copy pathcustom_stack.py
File metadata and controls
47 lines (37 loc) · 1.02 KB
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
46
47
from typing import Any
class Stack(list):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.size = len(self)
def push(self, __object: Any) -> None:
try:
ans = super().append(__object)
self.size += 1
return ans
except Exception as e:
raise e
def append(self, __object: Any) -> None:
return self.push(__object)
def pop(self):
try:
ans = super().pop()
self.size -= 1
return ans
except Exception as e:
raise e
def __getitem__(self, *args, **kwargs):
raise NotImplementedError
def clear(self) -> None:
return super().clear()
@property
def is_empty(self):
return self.size == 0
if __name__ == "__main__":
q = Stack()
print("q.is_empty : ", q.is_empty)
q.push("Hello")
q.clear()
print("q.is_empty : ", q.is_empty)
print(q)
print(q.pop())
print("q.is_empty : ", q.is_empty)