286. Parse Lisp Expression

HardStringString

Evaluate a Lisp-like expression returning an integer. Expressions are 'let', 'add', or 'mult' forms, or an integer, or a variable. 'add' and 'mult' take two sub-expressions; 'let' takes alternating variable/value pairs followed by a final expression, with proper lexical scoping. Input: a quoted expression.

Input: A quoted Lisp expression string.

Output: Integer — the evaluated value.

Examples

Example 1
Input: "(add 1 2)"
Output: 3
Explanation: 1 + 2.
Example 2
Input: "(mult 3 (add 2 3))"
Output: 15
Explanation: 3 * 5.
Example 3
Input: "(let x 2 (mult x 5))"
Output: 10
Explanation: x=2, 2*5.

Constraints

Asked by

AppleGoogle
Solve this problem in the editor →