-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm150.py
22 lines (21 loc) · 848 Bytes
/
m150.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
vals = []
for token in tokens :
if token.isnumeric() or len(token) > 1:
vals.append(token)
else :
val1, val2 = int(vals.pop()), int(vals.pop())
match token :
case '+' :
vals.append(str(val2 + val1))
case '-' :
vals.append(str(val2 - val1))
case '*' :
vals.append(str(val2 * val1))
case '/' :
if (val1 > 0) == (val2 > 0) :
vals.append(str(val2 // val1))
else :
vals.append(str(-1 * (abs(val2) // abs(val1))))
return int(vals.pop())