-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20-ReversePolish.py
More file actions
30 lines (28 loc) · 1.01 KB
/
20-ReversePolish.py
File metadata and controls
30 lines (28 loc) · 1.01 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
def evalRPN(tokens: list) -> int:
stack = []
ops = {'*', '/', '+', '-'}
for token in tokens:
if token in ops:
b = stack.pop()
a = stack.pop() # We can be sure there will always be at least 2, since the expression is guaranteed to be valid
# print(n)
if token == '*':
stack.append(a*b)
elif token == '/':
if (a < 0) ^ (b < 0):
stack.append(-(abs(a) // abs(b)))
else:
stack.append(a//b)
elif token == '+':
stack.append(a+b)
else:
stack.append(a-b)
else:
stack.append(int(token))
# print(stack)
return stack[-1]
if __name__ == "__main__":
print(evalRPN(["2", "1", "+", "3", "*"])) # 9
print(evalRPN(["4", "13", "5", "/", "+"])) # 6
print(evalRPN(["10", "6", "9", "3", "+", "-11", "*",
"/", "*", "17", "+", "5", "+"])) # 22