Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Give the code for the following functions (ie where it says YOUR CODE HERE). Thanks in advance. ### Variable occurrence in a variable def v_contains(self,

Give the code for the following functions (ie where it says "YOUR CODE HERE"). Thanks in advance.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

### Variable occurrence in a variable

def v_contains(self, var):

"""Returns True of var is the same as self, and False otherwise."""

### YOUR CODE HERE

V.__contains__ = v_contains

### Occurrence of a variable in an expression

def expr_contains(self, var):

### YOUR CODE HERE

Expr.__contains__ = expr_contains

### Variable replacement in variables

def v_replace(self, x, e):

"""If self is x, replaces all occurrences of x with e."""

### YOUR CODE HERE

V.replace = v_replace

class Expr(object): ***** Abstract class representing expressions name "expr" # Not used, but just to define it. 1 2 3 4 5 6 7 8 9 10 11 def _init_(self, *args): **An object is created by passing to the constructor the children" self.children args self.value None # The value of the expression self.child_values None # The values of the children; useful to have 12 def eval(self): ** Evaluates the expression." # First, we evaluate the children. self.child_values [c.eval() if isinstance(c, Expr) else c for c in self.children] # Then, we evaluate the expression itself. self.value = self.op(*self.child_values) return self.value def op(self): ** "* "This operator must be implemented in subclasses; it should compute self.value from self.values, thus implementing the operator at the expression node. ** raise NotImplementedError() 13 14 15 16 17 18 19 20 21 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 2: def repr_(self): "Represents the expression in a somewhat readable way." if len(self.children) 1: # Unary operators return "({}{})".format(self._class__.name, self.children[@]) elif len(self.children) return "({} {} {})".format( self.children[@], self._class__.name, self.children[1] ) # Catch-all. return "{}({})".format(self._class__name_ '.join(repr(c) for c in self.children)) # Expression constructors def _add_(self, other): return Plus(self, other) def _radd__(self, other): return Plus(self, other) def _sub__(self, other): return Minus(self, other) 50 51 52 def _rsub_(self, other): return Minus (other, self) def __mul_(self, other): return Multiply(self, other) def _rmul_(self, other): return Multiply (other, self) 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 def _truediv_(self, other): return Divide (self, other) def _rtruediv_(self, other): return Divide (other, self) def __neg_(self): return Negative(self) ### Variable occurrence in a variable def v_contains(self, var): ***"Returns True of var is the same as self, and False otherwise.' ### YOUR CODE HERE V.__contains v_contains ## Here you can also test your code. x = :VO) VO) print(x in x) print(x in y) ### Tests for variable occurrence 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 x = VO) y = v) assert xin x assert not x in y z = x assert xin z ### Occurrence of a variable in an expression def expr_contains (self, var): ### YOUR CODE HERE Expr. _contains expr_contains ## Here you can also test your code. 103 VO) EVO VO) e = x + (2 * y) Z = print(x in e) print(y in e) print(z in e) ## Tests for occurrence: 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 X = VO) y = VO Z = VO) e = x + (2 * y) assert xin e assert y in e assert z not in e ### Variable replacement in variables def v_replace(self, x, e): **** "If self is x, replaces all occurrences of x with e. ### YOUR CODE HERE V.replace v_replace ## Here you can also test your code X = VO) VO) Z VO) print(x print(y print(x x.replace(x, x)) X.replace(x, y)) x.replace(y, z)) 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 Z ## Tests for variable replacement in variables. X = VO) VO VO) assert x == x.replace(x,x) assert y X.replace(x, y) assert x == X.replace(y, z) assert x.replace(x, y).replace(y, z) Z ## Other tests for variable replacement. X = v VO) e = X.replace(x, y + 3 * x) x.assign(2) y.assign(3) assert e.eval() == 9 class Expr(object): ***** Abstract class representing expressions name "expr" # Not used, but just to define it. 1 2 3 4 5 6 7 8 9 10 11 def _init_(self, *args): **An object is created by passing to the constructor the children" self.children args self.value None # The value of the expression self.child_values None # The values of the children; useful to have 12 def eval(self): ** Evaluates the expression." # First, we evaluate the children. self.child_values [c.eval() if isinstance(c, Expr) else c for c in self.children] # Then, we evaluate the expression itself. self.value = self.op(*self.child_values) return self.value def op(self): ** "* "This operator must be implemented in subclasses; it should compute self.value from self.values, thus implementing the operator at the expression node. ** raise NotImplementedError() 13 14 15 16 17 18 19 20 21 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 2: def repr_(self): "Represents the expression in a somewhat readable way." if len(self.children) 1: # Unary operators return "({}{})".format(self._class__.name, self.children[@]) elif len(self.children) return "({} {} {})".format( self.children[@], self._class__.name, self.children[1] ) # Catch-all. return "{}({})".format(self._class__name_ '.join(repr(c) for c in self.children)) # Expression constructors def _add_(self, other): return Plus(self, other) def _radd__(self, other): return Plus(self, other) def _sub__(self, other): return Minus(self, other) 50 51 52 def _rsub_(self, other): return Minus (other, self) def __mul_(self, other): return Multiply(self, other) def _rmul_(self, other): return Multiply (other, self) 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 def _truediv_(self, other): return Divide (self, other) def _rtruediv_(self, other): return Divide (other, self) def __neg_(self): return Negative(self) ### Variable occurrence in a variable def v_contains(self, var): ***"Returns True of var is the same as self, and False otherwise.' ### YOUR CODE HERE V.__contains v_contains ## Here you can also test your code. x = :VO) VO) print(x in x) print(x in y) ### Tests for variable occurrence 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 x = VO) y = v) assert xin x assert not x in y z = x assert xin z ### Occurrence of a variable in an expression def expr_contains (self, var): ### YOUR CODE HERE Expr. _contains expr_contains ## Here you can also test your code. 103 VO) EVO VO) e = x + (2 * y) Z = print(x in e) print(y in e) print(z in e) ## Tests for occurrence: 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 X = VO) y = VO Z = VO) e = x + (2 * y) assert xin e assert y in e assert z not in e ### Variable replacement in variables def v_replace(self, x, e): **** "If self is x, replaces all occurrences of x with e. ### YOUR CODE HERE V.replace v_replace ## Here you can also test your code X = VO) VO) Z VO) print(x print(y print(x x.replace(x, x)) X.replace(x, y)) x.replace(y, z)) 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 Z ## Tests for variable replacement in variables. X = VO) VO VO) assert x == x.replace(x,x) assert y X.replace(x, y) assert x == X.replace(y, z) assert x.replace(x, y).replace(y, z) Z ## Other tests for variable replacement. X = v VO) e = X.replace(x, y + 3 * x) x.assign(2) y.assign(3) assert e.eval() == 9

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

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions