basic_calculator_ii

 1# @leet start
 2
 3from math import trunc
 4
 5
 6class Solution:
 7    def calculate(self, s: str) -> int:
 8        """
 9        This question asks us to evaluate a string which can contain numbers,
10        spaces, and the four arithmetic operators, '+', '-', '*', '/', where
11        the expression is provided in infix notation.
12
13        Let's take an example, of '3+ 5 / 2',
14
15        We first handle parsing of the 3, and keep that.
16        Then we parse the plus operator, and when we do that,
17        we want to add 3 onto the stack, and save the operator for later.
18        We then parse the next number, 5, and then parse the next operator, /.
19        When we parse '/', we want to evaluate it immediately, so we set it
20        as our previous op and then find 2, and evaluate it, to get 2.
21        Then we add it to the stack, and get 5.
22
23        Make sure to add any of the four operators at the end of our string
24        so we can handle the last expr.
25        """
26        curr = 0
27        op = "+"
28        stack = []
29
30        for c in s + "+":
31            if c.isspace():
32                continue
33            elif c.isdigit():
34                curr *= 10
35                curr += int(c)
36            else:
37                if op == "+":
38                    stack.append(curr)
39                elif op == "-":
40                    stack.append(-curr)
41                elif op == "*":
42                    top = stack.pop()
43                    stack.append(top * curr)
44                elif op == "/":
45                    top = stack.pop()
46                    stack.append(trunc(top / curr))
47                curr = 0
48                op = c
49
50        return sum(stack)
51
52
53# @leet end
54
55
56def test():
57    assert 2 + 2 == 4
class Solution:
 7class Solution:
 8    def calculate(self, s: str) -> int:
 9        """
10        This question asks us to evaluate a string which can contain numbers,
11        spaces, and the four arithmetic operators, '+', '-', '*', '/', where
12        the expression is provided in infix notation.
13
14        Let's take an example, of '3+ 5 / 2',
15
16        We first handle parsing of the 3, and keep that.
17        Then we parse the plus operator, and when we do that,
18        we want to add 3 onto the stack, and save the operator for later.
19        We then parse the next number, 5, and then parse the next operator, /.
20        When we parse '/', we want to evaluate it immediately, so we set it
21        as our previous op and then find 2, and evaluate it, to get 2.
22        Then we add it to the stack, and get 5.
23
24        Make sure to add any of the four operators at the end of our string
25        so we can handle the last expr.
26        """
27        curr = 0
28        op = "+"
29        stack = []
30
31        for c in s + "+":
32            if c.isspace():
33                continue
34            elif c.isdigit():
35                curr *= 10
36                curr += int(c)
37            else:
38                if op == "+":
39                    stack.append(curr)
40                elif op == "-":
41                    stack.append(-curr)
42                elif op == "*":
43                    top = stack.pop()
44                    stack.append(top * curr)
45                elif op == "/":
46                    top = stack.pop()
47                    stack.append(trunc(top / curr))
48                curr = 0
49                op = c
50
51        return sum(stack)
def calculate(self, s: str) -> int:
 8    def calculate(self, s: str) -> int:
 9        """
10        This question asks us to evaluate a string which can contain numbers,
11        spaces, and the four arithmetic operators, '+', '-', '*', '/', where
12        the expression is provided in infix notation.
13
14        Let's take an example, of '3+ 5 / 2',
15
16        We first handle parsing of the 3, and keep that.
17        Then we parse the plus operator, and when we do that,
18        we want to add 3 onto the stack, and save the operator for later.
19        We then parse the next number, 5, and then parse the next operator, /.
20        When we parse '/', we want to evaluate it immediately, so we set it
21        as our previous op and then find 2, and evaluate it, to get 2.
22        Then we add it to the stack, and get 5.
23
24        Make sure to add any of the four operators at the end of our string
25        so we can handle the last expr.
26        """
27        curr = 0
28        op = "+"
29        stack = []
30
31        for c in s + "+":
32            if c.isspace():
33                continue
34            elif c.isdigit():
35                curr *= 10
36                curr += int(c)
37            else:
38                if op == "+":
39                    stack.append(curr)
40                elif op == "-":
41                    stack.append(-curr)
42                elif op == "*":
43                    top = stack.pop()
44                    stack.append(top * curr)
45                elif op == "/":
46                    top = stack.pop()
47                    stack.append(trunc(top / curr))
48                curr = 0
49                op = c
50
51        return sum(stack)

This question asks us to evaluate a string which can contain numbers, spaces, and the four arithmetic operators, '+', '-', '*', '/', where the expression is provided in infix notation.

Let's take an example, of '3+ 5 / 2',

We first handle parsing of the 3, and keep that. Then we parse the plus operator, and when we do that, we want to add 3 onto the stack, and save the operator for later. We then parse the next number, 5, and then parse the next operator, /. When we parse '/', we want to evaluate it immediately, so we set it as our previous op and then find 2, and evaluate it, to get 2. Then we add it to the stack, and get 5.

Make sure to add any of the four operators at the end of our string so we can handle the last expr.

def test():
57def test():
58    assert 2 + 2 == 4