Function Expression Evaluator

Asked at GoogleOnsite

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

  1. Example 1

    Input: expression = add(2, mul(3, pow(4, 5)))

    Output: 3074

    Explanation: pow(4, 5) is 1024, mul(3, 1024) is 3072, and add(2, 3072) is 3074.

  2. Example 2

    Input: expression = add(1, sub(1, 0))

    Output: 2

    Explanation: sub(1, 0) is 1, and add(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.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.