-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate.py
51 lines (45 loc) · 1.61 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
46
47
48
49
50
51
class Solution:
def calculate(self, s: str) -> int:
"""
Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
"""
stack = []
num = 0
sign = 1
result = 0
for c in s:
if c.isdigit():
num = num * 10 + int(c)
elif c == '+':
result += sign * num
num = 0
sign = 1
elif c == '-':
result += sign * num
num = 0
sign = -1
elif c == '(':
stack.append(result)
stack.append(sign)
result = 0
sign = 1
elif c == ')':
result += sign * num
num = 0
result *= stack.pop()
result += stack.pop()
result += sign * num
return result
# Time complexity: O(N) where N is the length of the string s.
# Space complexity: O(N) where N is the length of the string s.
import unittest
class TestCalculate(unittest.TestCase):
def setUp(self) -> None:
self.solution = Solution()
def test_calculate(self) -> None:
self.assertEqual(self.solution.calculate("1 + 1"), 2)
self.assertEqual(self.solution.calculate(" 2-1 + 2 "), 3)
self.assertEqual(self.solution.calculate("(1+(4+5+2)-3)+(6+8)"), 23)
if __name__ == '__main__':
unittest.main()