-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevalrpn.py
35 lines (31 loc) · 901 Bytes
/
evalrpn.py
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
"""Eval Reverse Polish Notation
"""
def eval_rpn(tokens: list[str]) -> int:
"""
Space Complexity: O(1)
Time Complexity: O(n)
"""
stack = []
for token in tokens:
if token not in "+-*/":
stack.append(int(token))
continue
right, left = stack.pop(), stack.pop()
if token == "+":
stack.append(left + right)
elif token == "-":
stack.append(left - right)
elif token == "*":
stack.append(left * right)
elif token == "/":
stack.append(int(left / right))
return stack.pop()
if __name__ == "__main__":
print(eval_rpn(["2", "1", "+", "3", "*"]))
# => 9 => (2+1)*3
print(
eval_rpn(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"])
)
# => 22
print(eval_rpn(["4", "-2", "/", "2", "-3", "-", "-"]))
# => -7