-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path150.py
More file actions
34 lines (31 loc) · 990 Bytes
/
150.py
File metadata and controls
34 lines (31 loc) · 990 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
28
29
30
31
32
33
34
class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
import math
res = []
for i in tokens:
if i == '+' or i == '-' or i == '*' or i == '/':
res1 = float(res.pop())
res2 = float(res.pop())
if i == '/':
resnum = res2/res1
if resnum>0:
new_res = math.floor(resnum)
else:
new_res = math.ceil(resnum)
elif i == '+':
new_res = res2+res1
elif i == '-':
new_res = res2-res1
elif i == '*':
new_res = res2*res1
res.append(new_res)
else:
res.append(i)
return int(res[0])
solu = Solution()
print solu.evalRPN(
["10","6","9","3","+","-11","*","/","*","17","+","5","+"])