-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path150_evaluate_reverse_polish_notation.py
More file actions
37 lines (34 loc) · 1.15 KB
/
150_evaluate_reverse_polish_notation.py
File metadata and controls
37 lines (34 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
import math
class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack = []
for tok in tokens:
if tok == '+':
stack.append(stack.pop() + stack.pop())
elif tok == '*':
stack.append(stack.pop() * stack.pop())
elif tok == '-':
a, b = stack.pop(), stack.pop()
stack.append(b - a)
elif tok == '/':
a, b = stack.pop(), stack.pop()
stack.append(int(math.trunc(float(b) / a))) # for python3: stack.append(int(float(b) / a))
else:
stack.append(int(tok))
return stack[0]
vectors = [
[["18"], 18],
[["2","1","+","3","*"], 9],
[["4","13","5","/","+"], 6],
[["10","6","9","3","+","-11","*","/","*","17","+","5","+"], 22]
]
for vector in vectors:
params = vector[0]
expected = vector[1]
print(f'reverse polish notation is {params}, answer = {expected}')
returned = Solution().evalRPN(params)
assert returned == expected, f'for {params} expected {expected}, but returned {returned}!'