-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasicCalculator.py
88 lines (84 loc) · 2.53 KB
/
basicCalculator.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -* coding: utf-8 *-
'''
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23
'''
class Solution:
def calculate(self, s):
sol = Solution()
ans = 0
operands = []
operator = []
pivot = 0
i = 0
while i < len(s):
if s[i] == " ":
i += 1
continue
elif s[i].isnumeric():
j = i+1
tmp = s[i]
while j<len(s) and s[j].isnumeric():
tmp = tmp+s[j]
j += 1
continue
operands.append(int(tmp))
i = j
elif s[i] == "+":
if s[i+1] == "+":
operator.append("+")
i += 2
elif s[i+1] == "-":
operator.append("-")
i += 2
else:
operator.append(s[i])
i += 1
elif s[i] == "-":
if s[i+1] == "+":
operator.append("-")
i += 2
elif s[i+1] == "-":
operator.append("+")
i += 2
else:
operator.append(s[i])
i += 1
elif s[i] == "(":
pivot = i
i += 1
elif s[i] == ")":
s1 = s[:pivot]
if s[pivot+1:i].isnumeric():
s2 = s[pivot+1:i]
else:
s2 = str(sol.calculate(s[pivot+1:i]))
s3 = s[i+1:]
i += 1
try:
return int(s1+s2+s3)
except:
return sol.calculate(s1+s2+s3)
#else:
#return sol.calculate(s1+s2+s3)
#print(operands)
#print(operator)
for i in range(len(operator)):
if operator[i] == "+":
operands[i+1] = operands[i] + operands[i+1]
else:
operands[i+1] = operands[i] - operands[i+1]
return operands[-1]
"""
:type s: str
:rtype: int
"""
if __name__ == "__main__":
sol = Solution()
s = u"(1+(4+5+2)-3)+(6+8)"
print(sol.calculate(s))