Processing math: 100%

Problem 1: Polynomial (100pts)

In mathematics, a polynomial is an expression consisting of indeterminates (also called variables) and coefficients, that involves only the operations of addition, subtraction, multiplication, and non-negative integer exponentiation of variables. An example of a polynomial of a single indeterminate x is x24x+7.

We can use class Polynomial to represent polynomials, and a list of number to represent coefficients. For example, Polynomial([a_0, a_1, ..., a_n]) represents polynomial a0+a1x+...+anxn.

Implement required methods of class Polynomial such that representing a Polynomial object Polynomial([a_0, a_1, ..., a_n]) displays Polynomial([a_0, a_1, ..., a_n]), printing it displays a_0 + a_1*x^1 + ... + a_n*x^n and we can directly add or multiply two polynomial objects.

Note: the highest degree coefficient of a polynomial should not be 0, unless it is just 0.

Hint1: You can convert numbers to strings using the str function, and you can combine strings together using +.

Hint2: You may find python built-in function enumerate helpful.

class Polynomial: """Polynomial. >>> a = Polynomial([0, 1, 2, 3, 4, 5, 0]) >>> a Polynomial([0, 1, 2, 3, 4, 5]) >>> print(a) 0 + 1*x^1 + 2*x^2 + 3*x^3 + 4*x^4 + 5*x^5 >>> b = Polynomial([-1, 0, -2, 1, -3]) >>> print(b) -1 + 0*x^1 + -2*x^2 + 1*x^3 + -3*x^4 >>> print(a + b) -1 + 1*x^1 + 0*x^2 + 4*x^3 + 1*x^4 + 5*x^5 >>> print(a * b) 0 + -1*x^1 + -2*x^2 + -5*x^3 + -7*x^4 + -12*x^5 + -11*x^6 + -15*x^7 + -7*x^8 + -15*x^9 >>> print(a) 0 + 1*x^1 + 2*x^2 + 3*x^3 + 4*x^4 + 5*x^5 >>> print(b) # a and b should not be changed -1 + 0*x^1 + -2*x^2 + 1*x^3 + -3*x^4 >>> zero = Polynomial([0]) >>> zero Polynomial([0]) >>> print(zero) 0 """ "*** YOUR CODE HERE ***"

You may find Python string formatting syntax or f-strings useful. A quick example:

>>> ten, twenty, thirty = 10, 'twenty', [30] >>> '{0} plus {1} is {2}'.format(ten, twenty, thirty) '10 plus twenty is [30]' >>> feeling = 'love' >>> course = 61 >>> f'I {feeling} {course}A!' 'I love 61A!'