Develop class Polynomial. The internal representation of a Polynomial is a dictionary of terms. Each term is a key–value pair that contains an exponent and a coefficient. The term 2x4 has the coefficient 2 and the exponent 4. For simplicity, assume the polynomial contains only nonne- gative exponents. Develop the class with a dictionary-based interface for accessing terms that includes the
following elements:
a) The class’s constructor accepts a dictionary of exponent:coefficient pairs.
b) Coefficient values in a Polynomial are accessed by exponent keys
(e.g., polynomial[ exponent ] = coefficient). If a polynomial does not
have a coefficient for a specified exponent, the expression polynomial[ exponent ] evaluates to 0.
c) The length of a Polynomial is the value of its highest exponent.
d) Define method __str__ for representing a Polynomial as a string with terms of the form cxy.
e) Include an overloaded addition operator (+) to add two Polynomials.
f) Include an overloaded subtraction operator (-) to subtract two Polynomials.
```
# Definition of class Polynomial.
import copy
class Polynomial:
"""Class to represent a Polynomial as a dictionary"""
def __init__( self, initialDictionary = None ):
"""Initializes Polynomial instance"""
if not initialDictionary:
initialDictionary = {}
self._polyDict = copy.deepcopy( initialDictionary )
def __getitem__( self, power ):
"""Retrieve the coefficient for a power"""
if power in self.exponents():
return self._polyDict[ power ]
else:
return 0
def __setitem__( self, power, coefficient ):
"""Set a coefficient for a power"""
self._polyDict[ power ] = coefficient
def __len__( self ):
"""Returns highest power of polynomial"""
return max( self._polyDict.keys() )
def __str__( self ):
"""String representation of polynomial"""
terms = self._polyDict.items()
terms.sort()
terms.reverse()
termList = []
for power, coefficient in terms:
termList.append( "%dx^%d" % ( coefficient, power ) )
return " + ".join( termList )
def __add__( self, other ):
"""Add two Polynomial instances"""
newDict = copy.deepcopy( self._polyDict )
for power in range( max( len( self ), len( other ) ) + 1 ):
newDict[ power ] = self[ power ] + other[ power ]
return Polynomial( newDict )
def __sub__( self, other ):
"""Subtract two Polynomial instances"""
newDict = copy.deepcopy( self._polyDict )
for power in range( max( len( self ), len( other ) ) + 1 ):
newDict[ power ] = self[ power ] - other[ power ]
return Polynomial( newDict )
def __mul__( self, other ):
"""Multiply two Polynomial instances"""
newPoly = Polynomial()
for powerA, coefficientA in self.terms():
for powerB, coefficientB in other.terms():
newPoly[ powerA + powerB ] += \
coefficientA * coefficientB
return newPoly
def terms( self ):
"""Return list of terms in polynomial"""
return self._polyDict.items()
def exponents( self ):
"""Return list of exponents in polynomial"""
return self._polyDict.keys()
def coefficients( self ):
"""Return list of coefficients in polynomial"""
return self._polyDict.values()
```
>>> p1 = Polynomial( { 3 : 3, 2 : 2, 1 : 1, 0 : -1 } )
>>> p2 = Polynomial( { 2 : 4, 1 : 2 } )
>>> print p1
3x^3 + 2x^2 + 1x^1 + –1x^0
>>> print p2
4x^2 + 2x^1
>>> print p1 + p2
3x^3 + 6x^2 + 3x^1 + -1x^0
>>> print p1 - p2
3x^3 + -2x^2 + -1x^1 + -1x^0
>>> print p1 * p2
12x^5 + 14x^4 + 8x^3 + -2x^2 + -2x^1
You might also like to view...
Which of the symbols below is not a relational operator?
A. > B. && C. == D. !=
For a hash table with five slots, and using chaining to resolve collisions what does the inserted sequence: 35, 2, 18, 6, 3, 10, 8, 5 look like in the hash table for the hash function h(x) = x % 5?
a. [ (35, 10, 5) , (6), (2), (3,8) , () ] b. [ (5, 10, 35) , (2,6) , (3,8) , () ] c. [ (3, 5, 6), (10), (35), () ] d. [ (3), (5,6), (8, 10), (35) ]
The main panel of the ____ dialog box displays all of the possible settings for a given effect.
A. Options B. Tools C. Effects D. Views
The Windows operating system simplifies the process of working with documents and apps by organizing the manner in which you interact with the computer.?
Answer the following statement true (T) or false (F)