All problems
Basic Calculator I
Problem
Evaluate a basic arithmetic expression containing +, -, parentheses, and spaces.
Digits form multi-digit numbers, and a sign before a parenthesis applies to the whole group. Keep a current result, a pending sign, and a stack of outer results and signs. Subtraction is handled by adding the negated value.
Return the integer value of the expression.
Hint
On an opening parenthesis push the result so far and the current sign, then start fresh.
Test cases
case 1
in: ["1 + 1"]
expected: 2
case 2
in: [" 2-1 + 2 "]
expected: 3
case 3
in: ["(1+(4+5+2)-3)+(6+8)"]
expected: 23
case 4
in: ["-2+ 1"]
expected: -1
case 5
in: ["0"]
expected: 0
Starter codePython
def basic_calculator(s):
# Your code here
pass5 test cases · run your code against them in the editor