KatInterview

Evaluate a Nested Let/Add/Mult Expression, or Report Invalid

StringsAuto-graded · 28 test cases
MediumAuto-gradedStringsparsingrecursionscope

Last updated

Tokenize, validate and evaluate a small Lisp-like language with scoped let bindings, returning None for any malformed or ill-formed input.

You are given a string expr that is meant to hold one expression in a tiny Lisp-style language. Evaluate it and return the resulting integer. If the string is not a well-formed expression, or evaluating it would read an undefined variable, return None.

Language

An expression is one of the following:

  • Integer: an optional leading - followed by one or more ASCII digits (for example 7, -12, 007, -0). A leading + is not allowed.
  • Variable: a lowercase letter followed by any number of lowercase letters or digits (for example x, a1, add). Its value is the one bound by the nearest enclosing let that defines it.
  • Addition: (add e1 e2), which evaluates to the value of e1 plus the value of e2.
  • Multiplication: (mult e1 e2), which evaluates to the value of e1 times the value of e2.
  • Binding form: (let v1 e1 v2 e2 ... vk ek body), with k >= 1. Each vi is a variable name, each ei and body is an expression. Pairs are processed left to right. The expression ei is evaluated with the bindings v1..v(i-1) already visible. If a name is bound more than once in the same let, the later binding replaces the earlier one from that point on. The value of the form is the value of body, evaluated with all k bindings visible. Bindings made by a let are visible only inside that let and shadow any outer binding of the same name.

Tokens and well-formedness

  • The input is split into tokens: (, ), and maximal runs of characters that are neither whitespace nor parentheses. Whitespace only separates tokens and is otherwise ignored.
  • Every non-parenthesis token must be a valid integer or a valid variable, as defined above. Anything else (such as 1a, x_y, +5, ADD, abc!) makes the input invalid.
  • The words add, mult and let are operators only when they appear immediately after (. Anywhere else they are ordinary variable names, so (let add 1 add) is valid and evaluates to 1.
  • A parenthesized form must start with exactly one of the operators add, mult, let, have exactly the operands described above, and be closed by its matching ).
  • The whole string must be exactly one expression. An empty or all-whitespace string, extra tokens after the expression, and unmatched parentheses are all invalid.
  • Reading a variable that has no binding in scope is invalid. An error anywhere in the string makes the whole result None, even in a part that would not affect the final value.

Return the integer value of a valid expression, or None otherwise.

Constraints

  • expr is a string; its length may be zero.
  • A bare integer or a bare bound-in-scope variable is a valid expression, but a bare unbound variable is not.
  • Integers in the input and intermediate results may be arbitrarily large (Python integers).
  • Very deeply nested input must not crash the program; return None if it cannot be processed.

Examples

Example 1

Input: evaluate_lisp_or_none('(add 1 2)')

Output: 3

Example 2

Input: evaluate_lisp_or_none('(mult 3 (add 2 3))')

Output: 15

Example 3

Input: evaluate_lisp_or_none('(let x 2 (mult x 5))')

Output: 10

Example 4

Input: evaluate_lisp_or_none('(add 1)')

Output: None

Example 5

Input: evaluate_lisp_or_none('(foo 1 2)')

Output: None

Example 6

Input: evaluate_lisp_or_none('(add 1 2')

Output: None

Hints

Hint 1

Split the text into tokens first: a parenthesis is its own token, and everything else is a run of non-space, non-parenthesis characters. Validate each atom against the integer and variable patterns.

Hint 2

Write a recursive function that reads exactly one expression starting at a shared position and returns its value. Raise an exception on any violation and convert it to None once at the top level.

Hint 3

For let, give each form its own copy of the scope. Inside it, an item is the final body exactly when it is an expression followed by `)`; otherwise it must be a variable name followed by a value expression. Require at least one pair, and after the top-level expression check that no tokens remain.