-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_polish_notation.py
More file actions
41 lines (36 loc) · 1.09 KB
/
Copy pathreverse_polish_notation.py
File metadata and controls
41 lines (36 loc) · 1.09 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
from typing import List
import re
print("Hello World")
tokens = ["1","2","+","3","*","4","-"]
tokens=["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
store =[]
for exp in tokens:
print(store)
if exp=="+":
val1 = store.pop()
val2 = store.pop()
val = (val1)+(val2)
store.append(val)
elif exp=="*":
val1 = store.pop()
val2 = store.pop()
val = (val1)*(val2)
store.append(val)
elif exp=="-":
val1 = store.pop()
val2 = store.pop()
val = (val2)-(val1)
store.append(val)
elif exp=="/":
val1 = store.pop()
val2 = store.pop()
val = int(float(val2)/val1)
store.append(val)
else:
store.append(int(exp))
return store[0]
evaluate = Solution()
out1 = evaluate.evalRPN(tokens)
print(out1)