
At Meta, you may need to evaluate a lightweight arithmetic expression entered in an internal tool. Implement a basic calculator that evaluates a string expression containing integers, spaces, +, -, (, and ).
Write a function that takes a string s and returns an integer result.
s — a valid expression string"-1 + 2" or "1 - (-2)".Example 1
s = "1 + 1"22.Example 2
s = "2-(1+2)"-1(1+2)=3, then 2-3=-1.Example 3
s = "(1+(4+5+2)-3)+(6+8)"231 <= len(s) <= 3 * 10^5s contains digits, spaces, +, -, (, and ) onlys is a valid expressions = "1 + 1"Output2WhyThe two integers are added directly.s = "2-(1+2)"Output-1WhyThe parenthesized expression evaluates to 3, and 2 - 3 = -1.s = "(1+(4+5+2)-3)+(6+8)"Output23WhyNested parentheses are evaluated in order, and the final sum is 23.1 <= len(s) <= 3 * 10^5s contains digits, spaces, '+', '-', '(', and ')' onlys is a valid expressionIntermediate and final results fit in a 32-bit signed integerdef calculate(s):