|
| 1 | +#!/usr/bin/python3 |
| 2 | +""" |
| 3 | +Implement a basic calculator to evaluate a simple expression string. |
| 4 | +
|
| 5 | +The expression string contains only non-negative integers, +, -, *, / operators |
| 6 | +and empty spaces . The integer division should truncate toward zero. |
| 7 | +
|
| 8 | +Example 1: |
| 9 | +
|
| 10 | +Input: "3+2*2" |
| 11 | +Output: 7 |
| 12 | +Example 2: |
| 13 | +
|
| 14 | +Input: " 3/2 " |
| 15 | +Output: 1 |
| 16 | +Example 3: |
| 17 | +
|
| 18 | +Input: " 3+5 / 2 " |
| 19 | +Output: 5 |
| 20 | +Note: |
| 21 | +
|
| 22 | +You may assume that the given expression is always valid. |
| 23 | +Do not use the eval built-in library function. |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +class Solution: |
| 28 | + def calculate(self, s: str) -> int: |
| 29 | + """ |
| 30 | + No brackets. Look at previous operand and operator, when finishing |
| 31 | + scanning current operand. |
| 32 | + """ |
| 33 | + operand = 0 |
| 34 | + stk = [] |
| 35 | + prev_op = "+" |
| 36 | + for i, c in enumerate(s): |
| 37 | + if c.isdigit(): |
| 38 | + operand = operand * 10 + int(c) |
| 39 | + |
| 40 | + # i == len(s) - 1 |
| 41 | + delimited = c in ("+", "-", "*", "/") or i == len(s) - 1 |
| 42 | + if delimited: |
| 43 | + if prev_op == "+": |
| 44 | + cur = operand |
| 45 | + elif prev_op == "-": |
| 46 | + cur = -operand |
| 47 | + elif prev_op == "*": |
| 48 | + cur = stk.pop() * operand |
| 49 | + else: |
| 50 | + assert prev_op == "/" |
| 51 | + # instead of op1 // op2 due to negative handling, -3 // 2 == -2 |
| 52 | + cur = int(stk.pop() / operand) |
| 53 | + |
| 54 | + stk.append(cur) |
| 55 | + prev_op = c |
| 56 | + operand = 0 |
| 57 | + |
| 58 | + return sum(stk) |
| 59 | + |
| 60 | + def calculate_error(self, s: str) -> int: |
| 61 | + """ |
| 62 | + cannot use dictionary, since it is eager evaluation |
| 63 | + """ |
| 64 | + operand = 0 |
| 65 | + stk = [] |
| 66 | + prev_op = "+" |
| 67 | + for i, c in enumerate(s): |
| 68 | + if c.isdigit(): |
| 69 | + operand = operand * 10 + int(c) |
| 70 | + |
| 71 | + # i == len(s) - 1 |
| 72 | + delimited = c in ("+", "-", "*", "/") or i == len(s) - 1 |
| 73 | + if delimited: |
| 74 | + cur = { |
| 75 | + "+": operand, |
| 76 | + "-": -operand, |
| 77 | + "*": stk.pop() * operand, |
| 78 | + "/": int(stk.pop() / operand), # instead of op1 // op2 due to negative handling, -3 // 2 == -2 |
| 79 | + }[prev_op] |
| 80 | + stk.append(cur) |
| 81 | + |
| 82 | + prev_op = c |
| 83 | + operand = 0 |
| 84 | + |
| 85 | + return sum(stk) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + assert Solution().calculate("3+2*2") == 7 |
0 commit comments