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...
Answer the following statements true (T) or false (F)
1. A Sub procedure can call another Sub procedure. 2. Sub procedures can be called only once during the execution of a program. 3. Each parameter defined for a Sub procedure corresponds to an argument passed in a calling statement for that procedure. 4. Arguments and parameters can be used to pass values to Sub procedures from event procedures or other Sub procedures. 5. Parameters appearing in a Sub statement are part of the Sub procedure name.
A message _________________ contains the sender and recipient addresses date and subject line.
A. SMTP B. IMAP C. header D. DOCTYPE