def make_sum(a, b):
return ['+', a, b]
def make_prod(a, b):
return ['*', a, b]
def make_diff(a, b):
return ['-', a, b]
def make_div(a, b):
return ['/', a, b]
def is_basic_exp(a):
return not isinstance(a, list)
def eval_exp(e):
if is_basic_exp(e):
return e
op, a, b = e[0], eval_exp(e[1]), eval_exp(e[2])
if op == '+':
return eval_sum(a, b)
elif op == '-':
return eval_diff(a, b)
elif op == '*':
return eval_prod(a, b)
elif op == '/':
return eval_div(a, b)
else:
raise ValueError("Unkown opterator:", op)
def eval_sum(a, b):
if isinstance(a, Number) and isinstance(b, Number):
return a+b
if isinstance(a, Number) and a == 0:
return b
if isinstance(b, Number) and b == 0:
return a
return make_sum(a, b)
def eval_div(a, b):
if isinstance(a, Number) and isinstance(b, Number):
return a / b
if isinstance(a, Number) and a == 0:
return 0
if isinstance(b, Number) and b == 1:
return a
if isinstance(b, Number) and b ==0:
raise ZeroDivisionError
return make_div(a, b)
def eval_prod(a, b):
if isinstance(a, Number) and isinstance(b, Number):
return a * b
if isinstance(a, Number) and a == 0:
return 0
if isinstance(b, Number) and b == 0:
return 0
if isinstance(a, Number) and a == 1:
return b
if isinstance(b, Number) and b == 1:
return a
return make_prod(a, b)
def eval_diff(a, b):
if isinstance(a, Number) and isinstance(b, Number):
return a - b
if isinstance(a, Number) and a == 0:
return -b
if isinstance(b, Number) and b == 0:
return a
return make_diff(a, b)