Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I got this code (attached) for this problem: Write a Python class Polynomial which captures polynomials with integer coefficients. Your class must provide at least

I got this code (attached) for this problem:

Write a Python class Polynomial which captures polynomials with integer coefficients. Your class must provide at least the following features:

-a method to create a polynomial by specifying a list of coefficients and a variable name as string, e.g., p=Polynomial([1,-2,3],"x") must create the polynomial 1-2x+3x2. If the coefficient list contains any value that is not an integer, a TypeError must be raised.

-an implementation of the addition operator + to add polynomials that have the same variable

-conversion of a Polynomial into a string, which must return a representation of the form "1-2*x+3*x^2".

image text in transcribed image text in transcribed

The _str_ function still doesn't work yet. I have been trying to write this code so that if I try to print a polynomial with several terms, it prints all of them. Please do not drastically change the code I have but simply adjust it so that is prints all of the polynomial terms.

*Please go step by step with explanation*

**

Dec.3.py C:/Users/HHGREG py (3.6.2) File Edit View Playback Help File Edit Format Run Options Window Help class Polynomial: def init (self, coefficients, variable): Takes arguments at time of object creation and assigns it to object.' self.coefficients coefficients self.variablevariable f r eff in coefficients: if type (coeff)int: raise TypeError("Invalid coefficients") def -add--(self, object): Adds two polynomial object if variables are same.''' if isinstance (object, Polynomial): if self.variableobject.variable: poly_suml o (pi1, p2) in zip(self.coefficients,object.coefficients): if len (poly sum)-len (self.coefficients): else: polysum.append (pi + p2) poly sum.extend (self.coefficients[len (polysum):1) poly sum. extend (object. coefficients [ len (poly-sum) : ] polynomial Polynomial (poly_ sum, self.variable) return polynomial else: raise TypeEzzor ('Cannot perform addition on different variables. def poly to string (self): Converts polynomial object to string.'" poly_string'' for idx, coeff in enumerate(self.coefficients) coefficient if coeff 0: coefficient- +' + str (coeff) else: if idx0 elif idx1: else: poly string str (coeff) poly_string+ coefficientself.variable poly string+ coefficientelf.variable str (idx) Nothing Left For You+ Sam Smith return poly string Ln: 74 Col: 36 12/3/2017

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_2

Step: 3

blur-text-image_3

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 Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions

Question

Explain the chemical properties of acids with examples.

Answered: 1 week ago

Question

Write the properties of Group theory.

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago