-
Notifications
You must be signed in to change notification settings - Fork 8
/
calculator.py
39 lines (36 loc) · 914 Bytes
/
calculator.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
import operator as op
class Solution(object):
def calculate(self, s):
"""
:type s: str
:rtype: int
"""
ops = {"*":op.mul, "/":op.floordiv, "+":op.add, "-":op.sub}
stk = []
l = len(s)
i = 0
while i<l:
j = i
while j<l and (s[j].isnumeric() or s[j] == " "):
j+=1
cur = int(s[i:j].strip())
if not stk:
stk.append(cur)
elif stk[-1] in {"*", "/"}:
curop = stk.pop()
prev = stk.pop()
stk.append(ops[curop](prev, cur))
else:
stk.append(cur)
if j<l and s[j] in ops:
stk.append(s[j])
j+=1
i = j
l = len(stk)
ans = stk[0]
i = 1
while i<l:
ans = ops[stk[i]](ans, stk[i+1])
i+=2
return ans
print(Solution().calculate("2*3-10"))