Tap here to switch tabs
Problem
Submissions

Problem: Parse Lisp Expression

med
30 min
Try to solve the Parse Lisp Expression problem.

Statement

Given a string expression representing a Lisp-like expression, evaluate the expression and return its integer value.

The syntax of an expression is defined as follows:

  • An expression can be an integer, a variable name, or a parenthesized subexpression.

  • A parenthesized subexpression begins with an operator, followed by its operands separated by single spaces:

    • (let var1 exp1 var2 exp2 ... varn expn expr): Assigns the value of each expression expi to the variable vari sequentially from left to right. Then, evaluates and returns the result of the final expression expr. Variables declared in this scope shadow variables with the same name in outer scopes, and the assignments only exist within this scope.

    • (add exp1 exp2): Evaluates expressions exp1 and exp2, returning their sum.

    • (mult exp1 exp2): Evaluates expressions exp1 and exp2, returning their product.

Constraints:

  • 1≤1 \leq expression.length ≤2000\leq 2000

  • There are no leading or trailing spaces in expression.

  • All tokens are separated by a single space in expression.

  • The answer and all intermediate calculations of that answer are guaranteed to fit in a 3232 bit signed integer.

  • The expression is guaranteed to be legal and evaluate to an integer.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Parse Lisp Expression

med
30 min
Try to solve the Parse Lisp Expression problem.

Statement

Given a string expression representing a Lisp-like expression, evaluate the expression and return its integer value.

The syntax of an expression is defined as follows:

  • An expression can be an integer, a variable name, or a parenthesized subexpression.

  • A parenthesized subexpression begins with an operator, followed by its operands separated by single spaces:

    • (let var1 exp1 var2 exp2 ... varn expn expr): Assigns the value of each expression expi to the variable vari sequentially from left to right. Then, evaluates and returns the result of the final expression expr. Variables declared in this scope shadow variables with the same name in outer scopes, and the assignments only exist within this scope.

    • (add exp1 exp2): Evaluates expressions exp1 and exp2, returning their sum.

    • (mult exp1 exp2): Evaluates expressions exp1 and exp2, returning their product.

Constraints:

  • 1≤1 \leq expression.length ≤2000\leq 2000

  • There are no leading or trailing spaces in expression.

  • All tokens are separated by a single space in expression.

  • The answer and all intermediate calculations of that answer are guaranteed to fit in a 3232 bit signed integer.

  • The expression is guaranteed to be legal and evaluate to an integer.