Question: Question) Give the code for the following functions where the class below is given: a) def compute_arithmetic(e), b) def simplify(e), c) def compute(e, varval={}), d)
Question) Give the code for the following functions where the class below is given: a) def compute_arithmetic(e), b) def simplify(e), c) def compute(e, varval={}), d) the defined variables code and e) def value_equality(e, f, num_samples=1000, tolerance=1e-6). It must follow the exact parameters and pass the tests assigned to each function below. Insert code where it says, "YOUR CODE HERE."







class IllegalExpression(Exception): pass
def compute_plus_minus(e): if isinstance(e, tuple): # We have an expression. op, l, r = e # We compute the subexpressions. ll = compute_plus_minus(l) rr = compute_plus_minus(r) # And on the basis of those, the whole expression. if op == "+": return ll + rr elif op == "-": return ll - rr else: raise IllegalExpression(repr(e)) else: # base expression; just return the number. return e
compute_plus_minus(("+", 4, 5))
compute_plus_minus(("+", ("-", 3, 1), ("+", 4, 9)))
import traceback
try: compute_plus_minus(("^", 3, 4)) except: traceback.print_exc()
def calc(op, left, right): if op == "+": return left + right elif op == "-": 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
## Simple tests for one-level expressions.
assert compute_arithmetic(3) == 3 assert compute_arithmetic(("+", 3, 5)) == 8 assert compute_arithmetic(("-", 3, 5)) == -2 assert compute_arithmetic(("*", 3, 5.5)) == 16.5 assert compute_arithmetic(("/", 10, 5)) == 2
## Tests for multilevel expressions.
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
## Hidden tests for expressions.
def simplify(e): """Simplifies an expression containing variables, carrying out all possible computations.""" ### YOUR CODE HERE
## Tests for simplify.
e = ('+', 6, ('-', 7, 2)) assert simplify(e) == 11
e = ('+', 6, ('-', "x", 2)) assert simplify(e) == e
e = ('+', "cat", ('-', 7, 2)) assert simplify(e) == ('+', "cat", 5)
e = ('*', ('+', 2, 3), ('-', 7, 2)) assert simplify(e) == 25
e = ('*', ('+', 2, 3), ('-', 7, "monkey")) assert simplify(e) == ('*', 5, ('-', 7, 'monkey'))
## Hidden tests for simplify.
### Evaluating an expression with respect to a variable evaluation
def compute(e, varval={}): ### YOUR CODE HERE
## Tests for compute.
e = ('*', 2, ('+', 'x', ('-', 3, 2))) assert compute(e) == ('*', 2, ('+', 'x', 1)) assert compute(e, varval={'x': 6}) == 14 assert compute(e, varval={'y': 10}) == ('*', 2, ('+', 'x', 1))
e = ('+', ('-', 'yy', 3), ('*', 'x', 4)) assert compute(e, varval={'x': 2}) == ('+', ('-', 'yy', 3), 8) assert compute(e, varval={'yy': 3}) == ('+', 0, ('*', 'x', 4)) assert compute(e, varval={'x': 2, 'yy': 3}) == 8
### Hidden tests for compute.
### Exercise: define `variables`
### YOUR CODE HERE
### Tests for `variables`.
e = ('*', ('+', 'x', 2), ('/', 'x', 'yay')) assert variables(e) == {'x', 'yay'}
e = ('-', ('+', 'a', 2), ('*', 'c', 'c')) assert variables(e) == {'a', 'c'}
### Hidden tests for variables.
### Exercise: implementation of value equality
import random
def value_equality(e, f, num_samples=1000, tolerance=1e-6): """Return True if the two expressions self and other are numerically equivalent. Equivalence is tested by generating num_samples assignments, and checking that equality holds for all of them. Equality is checked up to tolerance, that is, the values of the two expressions have to be closer than tolerance. It can be done in less than 10 lines of code.""" ### YOUR CODE HERE
### Tests for value equality.
e1 = ('+', ('*', 'x', 1), ('*', 'y', 0)) e2 = 'x' assert value_equality(e1, e2)
e3 = ('/', ('*', 'x', 'x'), ('*', 'x', 1)) assert value_equality(e1, e3)
e4 = ('/', 'y', 2) assert not value_equality(e1, e4) assert not value_equality(e3, e4) assert not value_equality(e4, e3)
e5 = ("+", "cat", ("-", "dog", "dog")) assert value_equality(e5, "cat") assert value_equality("cat", e5)
e6 = ("-", "hello", "hello") assert value_equality(e6, 0) assert value_equality(0, e6)
### Hidden tests for value equality.
] 1 class IllegalExpression(Exception): 2 pass [] 1 def compute_plus_minus(e): 2 if isinstance(e, tuple): 3 # We have an expression. 4 op, l, r = e 5 # We compute the subexpressions. 6 11 compute_plus_minus(1) 7 rn = compute_plus_minus(r) 8 # And on the basis of those, the whole expression. 9 if op == "+": 10 return ll + rr == "-": 12 return ll - rr else: raise IllegalExpression(repr(e)) 15 else: # base expression; just return the number. return e 18 BEB # EE ovou own elif op 1 compute_plus_minus(("+", 4, 5)) 2 [] 1 compute_plus_minus (("+", ("-", 3, 1), ("+", 4, 9))) We can try the exception: [] 1 import traceback 2 B try: 4 compute_plus_minus(("*", 3, 4)) - 5 excepti traceback.print_exc() 6 7 Extend the compute_plus_minus to a compute_arithmetic function that can deal with all four arithmetic operators. You may use the above calc helper if you wish. [ 1 def compute arithmetic(e) : *****Computes the value of an arithmetic expression e. 3 ### YOUR CODE HERE *** 1 ## Simple tests for one-level expressions. 2 points. 2 3 assert compute_arithmetic(3) == 4 assert compute_arithmetic(("+", 3, 5)) 5 assert compute_arithmetic(("-", 3, 5)) 6 assert compute_arithmetic(("", 3, 5.5)) == 16.5 7 assert compute_arithmetic("/", 10, 5)) == 2 == 8 == -2 3 points. 1 ## Tests for multilevel expressions. 2 3 e = ("-", ("+", 3, 4), ("*", 5, 3)) 4 assert compute_arithmetic(e) == -8 5 6 e = ("*", ("/", 8, 4), ("*", 5, 3)) 7 assert compute_arithmetic(e) == 30 8 9. - ("*", ("/", 8, 4), ("*", 5, 3.2)) 1e assert compute_arithmetic(e) == 32 12 e = (***, ("/", 8, 4), ("*", ("-", 9, 6), 5)) 13 assert compute arithmetic(e) == 30 [] 1 ## Hidden tests for expressions. 2. 5 points. Write a version of simplify that is able to carry on computation on multi-level expressions, including (but not limited to) ('+', 6, ('-', 7, 2)). 1 def simplify(e): 2 ***Simplifies an expression containing variables, carrying out all possible computations.' 3 ### YOUR CODE HERE HER 1 ## Tests for simplify. 2 points. 3 e = ('+', 6, ('-', 7, 2)) 4 assert simplify(e) == 11 5 6 e = ('+', 6, ('-', "x", 2)) 7 assert simplify(e) 8 9e = ('+', "cat", ('-', 7, 2)) 10 assert simplify(e) ('+', "cat", 5) == e == 12 e = ('*', ('+', 2, 3), ('-', 7, 2)) 13-assert simplify(e) 25 14 15 e = ('*', ('+', 2, 3), ('-', 7, "monkey")) 16 assert simplify(e) (*', 5, ('-', 7, 'monkey')) [] 1 ## Hidden tests for simplify. 4 points. 2 [] 1 ### Evaluating an expression with respect to a variable evaluation 1. Da Lat 3 def compute(e, varval={}): ### YOUR CODE HERE [ ] 1 ## Tests for compute. 4 points. 3 e = ('*', 2, ('+', 'x', ('-', 3, 2))) 4 assert compute(e) ('*', 2, ('+', 'x', 1)) 5 assert compute(e, varval={'X': 6}) 6 assert compute(e, varval={ 'y': 10}) ('*', 2, ('+', 'x', 1)) == 14 8 e = ('+', ('-', 'yy', 3), ('*', 'x', 4)) 9 assert compute(e, varval={'X': 2}) == ('+', (':', 'yy', 3), 8) 10 assert compute(e, varval={'yy': 3}) ('+', 0, ('*', 'x', 4)) 11 assert compute(e, varval={'X': 2, "yy': 3}) 1 ### Hidden tests for compute. 6 points. 1 ### Exercise: define variables 3 ### YOUR CODE HERE 4 1 ### Tests for "variables. 2 points. 2 3 e = ('', ('+', 'x', 2), ('7', 'x', 'yay')) 4 assert variables (e) {'x', 'yay'} 5 6 e = ('-', ('+', 'a', 2), ('*', 'c', 'c')) 7 assert variables (e) {'a', 'c'} 1 ### Hidden tests for variables. 4 points. 2 If you can repeat the process num_sample times, and e and f are considered equal every time, then you declare them equal. 6 1 ### Exercise: implementation of value equality 2 3 import random 4 5 def value_equality(e, f, num_samples=1000, tolerance=1e-6): "Return True if the two expressions self and other are numerically 7 equivalent. Equivalence is tested by generating 8 num_samples assignments, and checking that equality holds 9 for all of them. Equality is checked up to tolerance, that is, 10 the values of the two expressions have to be closer than tolerance. 11 It can be done in less than 10 lines of code. 12 ### YOUR CODE HERE 13 [] 1 ### Tests for value equality. 4 points. 2 3 e1 = ('+", ("*', 'x', 1), ('*', 'y', 0)) 4 e2 = 'x' 5 assert value_equality(e1, e2) 6 7 e3 = ('/', ('*', 'x', 'x'), ('*', 'x', 1)) 8 assert value_equality(e1, e3) 9 10 e4 = ('7', 'y', 2) 11 assert not value_equality(e1, e4) 12 assert not value_equality(e3, 44) 13 assert not value_equality(e4, e3) 14 15 e5 = ("+", "cat", ("-", "dog", "dog")) 16 assert value_equality(e5, "cat") 17 assert value_equality("cat", e5) 18 19 e6 = ("-", "hello", "hello") 20 assert value_equality(e6, 0) 21 assert value_equality(0, e6) [] 1 ### Hidden tests for value equality. 6 points. 2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
