-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_stack_without_class.py
More file actions
98 lines (91 loc) · 1.66 KB
/
python_stack_without_class.py
File metadata and controls
98 lines (91 loc) · 1.66 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
# Title: Stack without a class
#
# Q:
# Implement a stack which takes only integers with 5 basic commands.
# Below is the list of commands
#
# push x : push the item x into the stack.
# pop : pop the item located on the top of the stack and print it out. If the stack was initially empty, print -1.
# size : print the size of stack.
# empty : print 1 if the stack is empty, otherwise print 0.
# top: print the top element from the stack. If the stack was initally empty, print -1.
#
#
#
#
# Input:
# The first line will contain the number of commands. Then from the second line, commands will be listed.
#
# Input Example:
# 14
# push 1
# push 2
# top
# size
# empty
# pop
# pop
# pop
# size
# empty
# pop
# push 3
# empty
# top
#
#
#
# Output:
# Print out the result of commands by line if there needs some print out
#
# Expected Output:
# 2
# 2
# 0
# 2
# 1
# -1
# 0
# 1
# -1
# 0
# 3
#
# A:
import sys
def push(x, stack):
stack.append(x)
def pop(stack):
if len(stack) == 0:
return -1
else:
element = stack[-1]
stack.pop()
return element
def size(stack):
return len(stack)
def empty(stack):
if len(stack) == 0:
return 1
else:
return 0
def top(stack):
if len(stack) == 0:
return -1
else:
return stack[-1]
stack = []
number_of_orders = int(sys.stdin.readline().rstrip("\n"))
for i in range(number_of_orders):
order = sys.stdin.readline().rstrip("\n")
order_buffer = order.split()
if order_buffer[0] == "push":
push(int(order_buffer[1]), stack)
elif order_buffer[0] == "pop":
print(pop(stack))
elif order_buffer[0] == "size":
print(size(stack))
elif order_buffer[0] == "empty":
print(empty(stack))
elif order_buffer[0] == "top":
print(top(stack))