-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTACK_SIM.py
42 lines (40 loc) · 847 Bytes
/
STACK_SIM.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
"""
Perform a sequence of operations over a stack, each element is an integer:
PUSH v: push a value v into the stack
POP: remove an element out of the stack and print this element to stdout (print NULL if the stack is empty)
Input:
Each line contains a command (operration) of type:
+ PUSH v
+ POP
Output:
Write the results of POP operations (each result is written in a line)
Example
Input:
PUSH 1
PUSH 2
PUSH 3
POP
POP
PUSH 4
PUSH 5
POP
#
Output:
3
2
5
"""
inp = "."
p = []
while(inp != "#"):
inp = input()
if inp=="#":
break
if "PUSH" in inp:
p.append(inp[5:])
else:
if len(p) > 0:
print(p[len(p)-1])
p.remove(p[len(p)-1])
else:
print("NULL")