In the Lisp language, a numerical expression is given as a parenthesized expression starting with a mathematical operator and followed by one or more operands. For example, the expression (+ 5 (* 3 2) 7)
evaluates to the value 18
.
Your task is to implement code that evaluates a Lisp style expression. You may assume that each expression has at most 1000 characters, that each operator is either +
or *
, and that any value arising during the calculation is in the range 0 \dots 10^{18}.
In a file lisp.py
, implement a function eval
that returns the value of the expression.
def eval(s): # TODO if __name__ == "__main__": print(eval("(+ 1 2 3 4 5)")) # 15 print(eval("(+ 5 (* 3 2) 7)")) # 18 print(eval("(* (+ (+ 1 2) 3) (+ (* 4 5) 6 2))")) # 168 print(eval("(+ 123 456)")) # 579