Parse Lisp Expression
Real-world version: Evaluate the Batch Recipe MacroThe statement here is our own: the same inputs, outputs and technique.
A plant writes the quantities on its batch sheets in a tiny macro language, and the loader has to reduce each sheet to a single whole number.
A term is one of the following, and nothing else.
- An integer literal: an optional
-followed by one or more digits, such as12or-40. Its value is that integer. - A label: a lowercase letter followed by any number of lowercase letters and digits, such as
vorx2orfeedrate. The three wordsplus,timesandwithare reserved and are never used as labels. A label's value is the value currently in force for it. (plus A B), whose value is the value of termAadded to the value of termB.(times A B), whose value is the value of termAmultiplied by the value of termB.(with L1 A1 L2 A2 ... Ln An B), which puts labels into force. Afterwithcome one or more pairs, each a label followed by the term giving its value, and then one final termB. The value of the wholewithis the value ofB.
The rules for a with are these.
- The pairs take effect from left to right. Term
A1is reduced beforeL1is in force, termA2is reduced withL1already in force, termA3withL1andL2in force, and so on. TermBis reduced with allnlabels in force. - The same label may appear in more than one pair of a single
with. The later pair takes over from the point it appears onward. - A label put into force by a
withhides whatever value that label carried outside thewith, and only until thatwithis finished. Once thewithis done, the hidden outer value is back in force.
program holds one term. Tokens are separated by single spaces, there is no space just inside a bracket, and there is no leading or trailing space. Every label used as a term has a value in force at the point it is used. Parse the text yourself; do not hand it to a language evaluator such as eval.
Return the value of program.
Examples
Example 1
- Input
- program = "(with base 5 span (with base 9 (times base 2)) (plus base span))"
- Output
- 23
The inner `with` puts `base` at 9 only inside itself, so its body `(times base 2)` is 18 and `span` becomes 18. The inner `with` is then finished, so `base` is back at 5 and the body `(plus base span)` is 5 + 18.
Example 2
- Input
- program = "(with a 3 b (plus a 4) (times a b))"
- Output
- 21
The pair for `b` is reduced with `a` already at 3, so `b` becomes 3 + 4 = 7, and the body `(times a b)` is 3 * 7.
Example 3
- Input
- program = "(with x2 7 (plus x2 x2))"
- Output
- 14
`x2` is a legal label: it starts with a letter and then carries a digit. It is put at 7, so the body `(plus x2 x2)` is 7 + 7.
Constraints
- 1 <= program.length <= 2000
- Tokens in program are separated by single spaces, and program has no leading or trailing space.
- program is a legal term and its value is an integer.
- The value of program and of every term reduced along the way fits in a signed 32-bit integer.
- Every label starts with a lowercase letter and continues with lowercase letters and digits; plus, times and with are never used as labels.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def evaluate_recipe(program: str) -> int:public int evaluateRecipe(String program)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 23 test cases: 23 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Decode StringReal-world version: Expand the Weave PatternMedium
- Using a Robot to Print the Lexicographically Smallest StringReal-world version: Typesetter's RackMedium
- Lexicographically Minimum String After Removing StarsReal-world version: Clearing the Markers Off the StripMedium
- Basic CalculatorReal-world version: Bracketed Offset TotalHard