Question

Write a Python class called polynomial that represents a polynomial in the following fashion, The initializer of your polynom


In 1: pe = polynomial( -1,-1,-1, 0, 0, -1) student answer = pe eval(2) correct answer -57 assert student answer = correct ans

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class polynomial: # A class that represents a polynomial is_print_zero_terms=True def __init__(self, *coefficients): self.coe#PLEASE LEAVE A THUMBS UP

align below program like above it works perfect a tested it

class polynomial:
"""
A class that represents a polynomial
"""
is_print_zero_terms=True
def __init__(self,*coefficients):
self.coefficients=list(coefficients)
  
def eval(self,x):
"""
Evaluates and return numeric value of this polynomial
"""
result = 0
for index, coeff in enumerate(self.coefficients[::-1]):
result += coeff * x** index
return result
def __str__(self):
count=len(self.coefficients) - 1
poly=''
for coeff in self.coefficients:
if coeff!=0:
if count<len(self.coefficients)-1:
if coeff<0:
poly=poly+" - "
else:
poly=poly+" + "
poly=poly+str(coeff)+".x^"+str(count)
else:
if polynomial.is_print_zero_terms==True :
poly=poly+" + "+str(coeff)+".x^"+str(count)
count-=1
return poly
  
pa = polynomial(1,1,1,0,1)
student_answer = pa.eval(2)
correct_answer = 29

assert student_answer == correct_answer

pe = polynomial(-1,-1,-1,0,0,-1)
student_answer = pe.eval(2)
correct_answer = -57

assert student_answer == correct_answer

polynomial.is_print_zero_terms = False

pg = polynomial(1,0,1,1,0,1)
student_answer = str(pg)
correct_answer = '1.x^5 + 1.x^3 + 1.x^2 + 1.x^0'

assert student_answer == correct_answer

Add a comment
Know the answer?
Add Answer to:
Write a Python class called polynomial that represents a polynomial in the following fashion, The initializer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this lab, you must define a class called Polynomial. This class definition must include the f...

    Please answer this in python 3, thank you. For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a...

  • For this lab, you must define a class called Polynomial. This class definition must include the f...

    please answer this question in python 3 For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a Polynomial...

  • in C++ as Exercise P12.14. Write a class Polynomial that stores a polynomial such p(x) =...

    in C++ as Exercise P12.14. Write a class Polynomial that stores a polynomial such p(x) = 5x10 + 9x7-x-10 as a linked list of terms. A term contains the coefficient and the example, you would store p(x) as power of x. For (5,10),(9,7),(-1,1),(一10,0) Supply member functions to add, multiply, and print polynomials. Supply a com- structor that makes a polynomial from a single term. For example, the polynomia; can be constructed as Polynomial p(Term(-10, 0)); p.add (Polynomial(Term(-1, 1))); p.add (Polynomial(Term(9,...

  • Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined...

    Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined in Java (1) Define a polynomial that has the following methods for Polynomial a. public Polynomial() POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0) POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p) POSTCONDITION: Creates a polynomial is the copy of p d. public void add_to_coef(double amount, int exponent) POSTCONDITION: Adds the given amount to...

  • In Java. Needing help with this homework assignment. The following is the class I need to...

    In Java. Needing help with this homework assignment. The following is the class I need to write up and implement. two maps. P15.3 Write a class Polynomial that stores a polynomial such as f(x) = 5x10 + 9x7-x-10 as a linked list of terms. A term contains the coefficient and the power of x. For example, you would store p(x) as (5,10),(9,7),(-1,1),(?10,0) Supply methods to add, multiply, and print polynomials. Supply a constructor that makes a polynomial from a single...

  • Which of the following versions of the polynomial class implementation provides the most space efficient representation...

    Which of the following versions of the polynomial class implementation provides the most space efficient representation for polynomials with a large degree and very few terms, for example: 10x+ x200. Recall that the degree of the polynomial is the largest exponent of the term with a non-zero coefficient. SELECT THE SINGLE BEST ANSWER A static array-based implementation, where only the coefficients are stored in the array, and the exponent of each term is the index of the coefficient in the...

  • You are to write a class called Point – this will represent a geometric point in...

    You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints). Point should have the following: Data:  that hold the x-value and the y-value. They should be ints and must be private. Constructors:  A default constructor that will set the values to (3, -5)  A parameterized constructor that will receive 2 ints (x then y) and set the data to what is...

  • Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials are...

    Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials are stored in a linked list of Term objects (two classes: Polynomial and Term). Term objects have two data fields: coefficient and exponent, both positive integers. Polynomial will have some methods that create linked list functionality, but not all list operations are needed; include only those used by this program. Do not use a separate linked list class. The list will be non-circular. Make a...

  • The second lab continues with the Polynomial class from the first lab by adding new methods...

    The second lab continues with the Polynomial class from the first lab by adding new methods for polynomial arithmetic to its source code. (There is no inheritance or polymorphism taking place yet in this lab.) Since the class Polynomial is designed to be immutable, none of the following methods should modify the objects this or other in any way, but return the result of that arithmetic operation as a brand new Polynomial object created inside that method. public Polynomial add(Polynomial...

  • A student is given the following polynomial and is asked to answer questions about it X5...

    A student is given the following polynomial and is asked to answer questions about it X5 - 198X+5x12+ 4 - 2x 1. The polynomial contains how many terms? 2 List all of the coefficients of the polynomial 3. What is the polynomial's constant? 4. What is the leading coefficient of the polynomial? 5. What is the degree of the polynomial? Here are the student's answers 1. The polynomial contains 5 terms 2. The coefficients are 198,5, 4-2 3. The constant...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT