-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_150.py
More file actions
27 lines (23 loc) · 788 Bytes
/
task_150.py
File metadata and controls
27 lines (23 loc) · 788 Bytes
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
from typing import List
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
opr = ['+', '-', '*', '/']
st = []
for c in tokens:
if c not in opr:
st.append(int(c))
continue
b, a = st.pop(), st.pop()
if c == '+':
st.append(a+b)
elif c == '-':
st.append(a-b)
elif c == '*':
st.append(a*b)
else:
st.append(int(a/b))
# print(st)
return st[0]
# print(Solution().evalRPN(tokens = ["2","1","+","3","*"]))
# print(Solution().evalRPN(tokens = ["4","13","5","/","+"]))
print(Solution().evalRPN(tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]))