-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path150.py
45 lines (33 loc) · 938 Bytes
/
150.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
36
37
38
39
40
41
42
43
44
45
'''150. Evaluate Reverse Polish Notation
Created on 2024-12-31 14:16:38
2024-12-31 14:46:34
@author: MilkTea_shih
'''
#%% Packages
#%% Variable
#%% Functions
class Solution:
def evalRPN(self, tokens: list[str]) -> int:
stack: list[int] = []
for token in tokens:
match token:
case '+':
stack[-1] = stack.pop() + stack[-1]
case '-':
stack[-1] = stack[-2] - stack.pop()
case '*':
stack[-1] = stack.pop() * stack[-1]
case '/':
stack[-1] = int(stack[-2] / stack.pop())
case _:
stack.append(int(token))
#
return stack[0]
#%% Main Function
Solution().evalRPN(
["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"],
)
#%% Main
if __name__ == '__main__':
pass
#%%