Function Expression Evaluator
Asked at Google
Reported by the problem's creator, not verified
You are given expression, a string that is either a non-negative integer or a call such as add(2, mul(3, 4)). A call's name is one of add, sub, mul, div and pow, and each of its arguments is itself an expression. Arguments are separated by commas, and a space may appear after a comma.
Implement evaluate(expression: str) -> int that returns the value of expression.
Examples
Example 1
Input: expression =
add(2, mul(3, pow(4, 5)))Output:
3074Explanation:
pow(4, 5)is 1024,mul(3, 1024)is 3072, andadd(2, 3072)is 3074.Example 2
Input: expression =
add(1, sub(1, 0))Output:
2Explanation:
sub(1, 0)is 1, andadd(1, 1)is 2.
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.