-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBasicCalculatorII_v1.py
More file actions
57 lines (47 loc) · 1.36 KB
/
BasicCalculatorII_v1.py
File metadata and controls
57 lines (47 loc) · 1.36 KB
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
#!/usr/bin/env python
# encoding: utf-8
class Solution:
# @param {string} s
# @return {integer}
def calculate(self, s):
result = 0
for m in s.strip().split('+'):
r = None
for n in m.strip().split('-'):
v = self.cal(n)
if r is None:
r = v
else:
r -= v
result += r if r else 0
return result
def cal(self, s):
numbers = []
ops = []
number = 0
for c in s:
if c.isdigit():
number = number * 10 + ord(c) - ord('0')
elif c == '*':
numbers.append(number)
ops.append('*')
number = 0
elif c == '/':
numbers.append(number)
ops.append('/')
number = 0
if number or s[-1] == '0':
numbers.append(number)
for i, op in enumerate(ops):
if op == '*':
numbers[i+1] = numbers[i] * numbers[i+1]
elif op == '/':
numbers[i+1] = numbers[i] // numbers[i+1]
return numbers[-1]
if __name__ == '__main__':
s = Solution()
print s.calculate('3+2*2')
print s.calculate(' 3/2 ')
print s.calculate(' 3+5 / 2 ')
print s.calculate('0')
print s.calculate('0+0')