-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcalculate.py
45 lines (32 loc) · 1 KB
/
calculate.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
class Solution:
def calculate(self, s: str) -> int:
stack = []
curr = 0
ope = "+"
if not s:
return 0
operators = set(['+', '-', '*', '/'])
nums = set([str(x) for x in range(0, 10)])
for i in range(len(s)):
char = s[i]
if char in nums:
curr = curr * 10 + int(char)
if char in operators or i == len(s) - 1:
if ope == '+':
stack.append(curr)
elif ope == '-':
stack.append(-curr)
elif ope == '*':
stack[-1] *= curr
elif ope == '/':
stack[-1] = int(stack[-1] / curr)
curr = 0
ope = char
return sum(stack)
# Checking in Terminal/Console:
if __name__ == '__main__':
Sol = Solution()
Solve = Sol.calculate(" 3+5 / 2 ")
# s = "3+2*2" -> 7
# s = " 3+5 / 2 " -> 5
print(Solve)