Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Provide the code for the following functions (ie where it says, YOUR CODE HERE). def compute_arithmetic(e): Computes the value of an arithmetic expression e. ###

Provide the code for the following functions (ie where it says, YOUR CODE HERE).

image text in transcribed

image text in transcribed

image text in transcribed

def compute_arithmetic(e):

"""Computes the value of an arithmetic expression e."""

### YOUR CODE HERE

def simplify(e):

"""Simplifies an expression containing variables, carrying out all possible computations."""

### YOUR CODE HERE

def compute(e, varval={}):

### YOUR CODE HERE

define `variables`

### YOUR CODE HERE

class IllegalExpression (Exception): pass rr 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 def compute_plus_minus(e): if isinstance(e, tuple): # We have an expression. op, 1, r = e # We compute the subexpressions. 11 compute_plus_minus(1) compute_plus_minus(r) # And on the basis of those, the whole expression. "+": return 11 + rr elif op return 11 - rr else: raise IllegalExpression(repr(e)) else: # base expression; just return the number. if op == return e def compute_arithmetic(e): ***** Computes the value of an arithmetic expression e. ### YOUR CODE HERE ## Simple tests for one-level expressions. == 8 assert compute_arithmetic(3) 3 assert compute_arithmetic(("+", 3, 5)) assert compute_arithmetic(("-", 3, 5)) -2 assert compute_arithmetic(("*", 3, 5.5)) 16.5 assert compute_arithmetic(("/", 10, 5)) == 2 ## Tests for multilevel expressions. ("-", ("+", 3, 4), ("*", 5, 3)) assert compute_arithmetic(e) -8 e = ("*", ("/", 8, 4), ("*", 5, 3)) assert compute_arithmetic(e) 30 ("*", ("/", 8, 4), ("*", 5, 3.2)) assert compute_arithmetic(e) 32 e = 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 ("*", ("/", 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. ('+', 6, ('-', 7, 2)) assert simplify(e) 11 ('+', 6, ('- assert simplify(e) "x", 2)) ('+', "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. 73 74 75 76 77 78 79 80 81 82 83 84 85 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)) 86 ('+', ('-', '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}) 87 88 89 90 91 ### Hidden tests for compute. 92 ### define "variables 93 94 ### YOUR CODE HERE ### Tests for variables. 95 96 97 98 99 100 101 102 103 104 ('*', ('+', 'x', 2), (7, 'x', 'yay')) assert variables (e) = {'x', 'yay'} e = ('-', ('+', 'a', 2), (**', 'c', 'c')) assert variables (e) {'a', 'c'} ### Hidden tests for variables. class IllegalExpression (Exception): pass rr 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 def compute_plus_minus(e): if isinstance(e, tuple): # We have an expression. op, 1, r = e # We compute the subexpressions. 11 compute_plus_minus(1) compute_plus_minus(r) # And on the basis of those, the whole expression. "+": return 11 + rr elif op return 11 - rr else: raise IllegalExpression(repr(e)) else: # base expression; just return the number. if op == return e def compute_arithmetic(e): ***** Computes the value of an arithmetic expression e. ### YOUR CODE HERE ## Simple tests for one-level expressions. == 8 assert compute_arithmetic(3) 3 assert compute_arithmetic(("+", 3, 5)) assert compute_arithmetic(("-", 3, 5)) -2 assert compute_arithmetic(("*", 3, 5.5)) 16.5 assert compute_arithmetic(("/", 10, 5)) == 2 ## Tests for multilevel expressions. ("-", ("+", 3, 4), ("*", 5, 3)) assert compute_arithmetic(e) -8 e = ("*", ("/", 8, 4), ("*", 5, 3)) assert compute_arithmetic(e) 30 ("*", ("/", 8, 4), ("*", 5, 3.2)) assert compute_arithmetic(e) 32 e = 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 ("*", ("/", 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. ('+', 6, ('-', 7, 2)) assert simplify(e) 11 ('+', 6, ('- assert simplify(e) "x", 2)) ('+', "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. 73 74 75 76 77 78 79 80 81 82 83 84 85 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)) 86 ('+', ('-', '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}) 87 88 89 90 91 ### Hidden tests for compute. 92 ### define "variables 93 94 ### YOUR CODE HERE ### Tests for variables. 95 96 97 98 99 100 101 102 103 104 ('*', ('+', 'x', 2), (7, 'x', 'yay')) assert variables (e) = {'x', 'yay'} e = ('-', ('+', 'a', 2), (**', 'c', 'c')) assert variables (e) {'a', 'c'} ### Hidden tests for variables

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

Are tension or anxiety levels in the group high?

Answered: 1 week ago

Question

What is churn, and how does it affect a firm?

Answered: 1 week ago

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago