Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help with my python homework about Expressions as Classes asap please and thank you :) i will also upvote if you answer PLEASE

Can someone help with my python homework about Expressions as Classes asap please and thank you :) i will also upvote if you answer PLEASE fill the ###Your Code Here

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Question 1

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

QUESTION 2

### Occurrence of a variable in an expression

def expr_contains(self, var):

### YOUR CODE HERE

Expr.__contains__ = expr_contains

- Defining an expression class We define an abstract class Expr, representing a generic expression. This generic class has as subclasses the classes that represent the various operators, such as Plus, Minus, Multiply, as well as the variable class v. Here is our basic implementation. class Expr(object) ""Abstract class representing expressions" name = "expr" # Not used, but just to define it. 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 det eval(self): Evaluates the expression." # First, we evaluate the children. self.child_values = [c.eval() if isinstance(C, Expr) else for c in self.children] # Then, we evaluate the expression itself. self.value = self.op(*self.child_values) return self.value def op(sel+): ***This operator must be implemented in subclasses; it should compute self.value from self.values, thus implementing the operator at the expression node." raise Not ImplementedError() det __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) == 2: return "({}{} })".Format( self.children[@], self._class__.name, self.children(1) # Catch-all. return "{}(0)".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) def __sub_(self, other): return Minus (other, selt) det __tul_(self, other): return Multiply(self, other) det rul_(self, other): return Multiply(other, self) det __truediv_(self, other): return Divide(self, other) def _rtruediv_(self, other): return Divide (other, self) def __neg__(self): return Negative(self) Variables are created specifying a name, and an initial value. If nothing is specified, variables have random initial values. You can assign a value to a variable using the assign method. The eval method of a variable simply returns its value. [24] import randon import string class V(Expr): - "Variable." der _init__(self, value=None): super() . __init_o self.children - self.value = randon.gauss(e, 1) if value is None else value self.name = ".join( randon.choices(string.ascii_letters + string.digits, k=3) det eval(selt): return self.value def assign(self, value): self.value = value def __repr_(self): return "co, value={})".Format(self.name, self.value) Here are the constructors for the other operators; for them, we just need to provide an implementation for op, since all the rest is inherited from Expr. We actually also define a name, as a class attribute, that we use in the representation method. [25] class Plus (Expr): name = " det op(self, x, y): return x + y class Minus (Expr): name = "." det op(self, x, y): return x - y class Multiply(Expr): name = "*" det op(selt, x, y): return xy class Divide(Expr): name = "/" det op(self, x, y): return x/y class Negative(Expr): name = "." def opselt, x): return - Alan innby Why is the result False? Python knows how to compare objects that belong to its own types. So you can do comparisons between strings, numbers, tuples, and more, and it all works as expected. This is why we could check equality of expressions represented as trees: those expression trees are composed entirely of standard Python types, namely, strings, numbers, and tuples. However, Expr, v, etc, are classes we defined, and Python has no idea of what it means for objects of user-defined classes to be equal. In this case, Python defaults to considering equal two objects if they are the same object. The two expressions et and e2 above are not the same object: they are two distinct objects, which just happen to represent the same expression. If we want to have a notion of expression equality that represents our idea that two expression objects are equal if they represent the same expression", we need to define equality ourselves. This can be easily done, by defining an__eq -- method. This method has the form def _c9__(self, other): return

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

Database Support For Data Mining Applications Discovering Knowledge With Inductive Queries Lnai 2682

Authors: Rosa Meo ,Pier L. Lanzi ,Mika Klemettinen

2004th Edition

3540224793, 978-3540224792

More Books

Students also viewed these Databases questions