-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
110 lines (85 loc) · 2.06 KB
/
Copy pathstack.py
File metadata and controls
110 lines (85 loc) · 2.06 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from collections import deque
import platform
import os
# Just to clear the output
def clear_console():
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
class Stack():
def __init__(self):
self.container = deque()
def append(self, item):
self.container.append(item)
def pop(self):
return self.container.pop()
def pop_left(self):
return self.container.popleft()
def remove(self, item):
self.container.remove(item)
def extend(self, items):
self.container.extend(items)
def peek(self):
return self.container[-1]
def is_empty(self):
return len(self.container) == 0
def size(self):
return len(self.container)
def print(self):
print(str(self.container)[6:-1] + "\n\n")
cmd = f"""[1] Append [2] Pop
[3] Pop left [4] Remove
[5] Extend [6] Peek
[7] Is empty [8] size
[9] Print [10] Exit\n
Enter : """
if __name__ == "__main__":
stk = Stack()
inp = 0
while inp != 10:
inp = 0
try:
inp = int(input(cmd))
except (KeyboardInterrupt, EOFError):
clear_console()
print("Exiting...\n")
break
except ValueError:
clear_console()
print("Enter a number...\n\n")
inp = 0
continue
if inp == 1:
item = int(input("Enter a number to append: "))
clear_console()
stk.append(item)
stk.print()
elif inp == 2:
clear_console()
print(f"Item: {stk.pop()} is popped from stack.\n\n")
elif inp == 3:
clear_console()
print(f"Item: {stk.pop_left()} is popped from stack.\n\n")
elif inp == 4:
item = int(input("Enter the item you want to remove : "))
stk.remove(item)
clear_console()
stk.print()
elif inp == 5:
item = input("Enter the items to extend : ").split(' ')
item = list(map(int, item))
stk.extend(item)
clear_console()
stk.print()
elif inp == 6:
print(f"{stk.peek()}\n\n")
elif inp == 7:
clear_console()
print(f"Stack is {'' if not stk.is_empty() else 'not'} empty.\n\n")
elif inp == 8:
clear_console()
print(f"The size of the stack is : {stk.size()}\n\n")
elif inp == 9:
clear_console()
stk.print()