Question: need the ###write your code here part class IllegalExpression (Exception): pass rr = def compute_plus_minus(e): if isinstance(e, tuple): # We have an expression. op, l,
need the ###write your code here part



class IllegalExpression (Exception): pass rr = def compute_plus_minus(e): if isinstance(e, tuple): # We have an expression. op, l, r = e # We compute the subexpressions. 1l = compute_plus_minus (1) compute_plus_minus (r) # And on the basis of those, the whole expression. if op == "+": return ll + rr elif op == return li else: raise IllegalExpression (repr(e)) else: # base expression; just return the number. return e rr op == "-": def calc(op, left, right): if op == "+": return left + right elif return left right elif op == = "*": return left * right elif op == "/": return left / right def compute_arithmetic(e): "" "Computes the value of an arithmetic expression e. ### YOUR CODE HERE II II 11 ## Simple tests for one-level expressions. 2 points. 3 == 8 == assert compute_arithmetic(3) == assert compute_arithmetic(("+", 3, 5)) assert compute_arithmetic(("-", 3, 5)) assert compute_arithmetic(("*", 3, 5.5)) assert compute_arithmetic(("/", 10, 5)) -2 == 16.5 == 2 e = ("-", ("+", 3, 4), ("*", 5, 3)) assert compute_arithmetic(e) -8 == e = ("*", ("/", 8, 4), ("*", 5, 3)) assert compute_arithmetic(e) == 30 e = ("*", ("/", 8, 4), ("*", 5, 3.2)) assert compute_arithmetic(e) 32 == e = ("*", ("/", 8, 4), ("*", ("-", 9, 6), 5)) assert compute_arithmetic(e) == 30
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
