Problem 2. Polynomials Design and implement in file p2.py) a class called Poly f
ID: 3722538 • Letter: P
Question
Problem 2. Polynomials Design and implement in file p2.py) a class called Poly for representing and operating with A polynomial in variable X of degree has this general expression: with coefficients Q,ER and a,0 Find out more about polynomials at www.math.auckland a) The Poly class must support the following operations, illustrated with examples below: Initialization, with the constructor taking an iterable (like a list , ) that generates the coefficients, starting with aa The coefficients are floats or ints, but the constructor must convert them to type float. The degree of the polynomial is given by the length of the sequence of the seqience Conversion to stringstr_. Skip terms aI*with coefficient -0. Use the default number of decals for floats. Representation, for printing at the terminal (rept_ · Indexing. This operation takes parameter k and returns the coefficient ak if 0 =n or throws . Addition with another Poly object add). · Testing for equality ( eq·_ne_). Two polynomials are equal if their coefficients are ValueError otherwise. If p is a Poly object p[k] returns agetitem_ Multiplication with another Poly object and with a float or an it. mul and rmul_) respectively equal Equal polynomials u be ot the same degree Evaluation of the polynomial for a given value for variable X. The method is called eval if x is an int or float then p.eval(x) returns the value of expression a ifxisa sequence of elementsx(an iterable, such as a tuple or a list), then p.eval(x) 1. Use a list returns a list with the matching elements [elf.eval(x), self.eval(xi), comprehension for this evaluation. b) Write in file p2.py a function called test poly that tests all operations and methods from part a) Use the function testif) from Homework 3 or something similar. Here are examples how Poly objects could be used from the Python shell: p2= Poly ( (0, 1)) print(pl pl pl= Poly ( ( 1.-2.1)) # creste poly of grade 1 with tuple: p2(X) X, { t) # print c11.-str-and print; 1.0-2.0XeX'2 # Python cells-repr-and di;plays 1.0-2.0x+x^2 # returns False return True # returns True # - print p3) # prints 1.0-X+X"2.0 (ust default number of decinals # indexing the coefficients: returns -2 (al for p1) # Product with another Poly: p4 becomes X-2X"2+X"3 # product with int or float: p5 becomes 2.4X+2X"2 # roduct with int or float: 6 becomes 3-GX43X"2 { rnul printl pl.evl(2) prant( pl.eval (te,-1.1]} # evaLuate pl for·list of points: prints [1.4.0] # evaluate plpoint 2: prints 1.0Explanation / Answer
class Poly: def __init__(self, coefficients): # Constructor self.coefficients = [float(coefficient) for coefficient in coefficients] def __str__(self): if len(self.coefficients) == 0: # If no coeffcients return '0' poly = '' if self.coefficients[0] != 0.0: # First coefficient poly += str(self.coefficients[0]) if self.coefficients[1] != 0.0: # Second coefficient if self.coefficients[1] < 0.0: # If negative poly += str(self.coefficients[1]) + 'X' else: # If positive poly += '+' + str(self.coefficients[1]) + 'X' index = 2 # From coefficient 2 to last while indexRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.