-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue_str.py
More file actions
55 lines (43 loc) · 1.15 KB
/
queue_str.py
File metadata and controls
55 lines (43 loc) · 1.15 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
class queue:
def __init__(self):
self.queue = []
def enqueue(self, val):
self.queue.insert(0, val)
def dequeue(self):
if self.is_empty():
return None
else:
return self.queue.pop()
#
def size(self):
return len(self.queue)
def is_empty(self):
return self.size() == 0
A = [1,4,5,10,2]
q = queue()
for i in A:
q.enqueue(i)
print(q.size())
class Solution:
# @param A : list of strings
# @return an integer
def evalRPN(self, A):
stack = []
for ch in A:
if ch in "+-*/":
arg2 = stack.pop()
arg1 = stack.pop()
if ch is "+":
stack.append(arg1 + arg2)
if ch is "-":
stack.append(arg1 - arg2)
if ch is "*":
stack.append(arg1 * arg2)
if ch is "/":
stack.append(arg1 // arg2)
else:
stack.append(int(ch))
return stack.pop()
A = [ "5", "1", "2", "+", "4", "*", "+", "3", "-" ]
Sols = Solution()
print(Sols.evalRPN(A))