class Polynomial: class TermNode: def __init__(self, coefficient: int, exponent: int) -> None: """Initialize this node to represent a polynomial term with the given coefficient and exponent. Raise ValueError if the coefficent is 0 or if the exponent is negative. """ if coefficient == 0: raise ValueError("TermNode: zero coefficient") if exponent < 0: raise ValueError("TermNode: negative exponent") self.coeff = coefficient self.exp = exponent self.next = None def __init__(self, coefficient: int = None, exponent: int = 0) -> None: """Initialize this Polynomial with a single term constructed from the coefficient and exponent. If one argument is given, the term is a constant coefficient (the exponent is 0). If no arguments are given, the Polynomial has no terms. # Polynomial with no terms: >>> p = Polynomial() >>> print(p._head) None >>> print(p._tail) None # Polynomial with one term (a constant): >>> p = Polynomial(12) >>> p._head.coeff 12 >>> p._head.exp 0 >>> print(p._head.next) None # Polynomial with one term: >>> p = Polynomial(12, 2) >>> p._head.coeff 12 >>> p._head.exp 2 >>> print(p._head.next) None """ # A polynomial is stored as a singly linked list. Each node stores # one term, with the nodes ordered in descending order, based on the # exponent. (The head node is the term with the highest exponent, # and the tail node is the term with the lowest exponent.) if coefficient is None and exponent == 0: self._head = None else: self._head = Polynomial.TermNode(coefficient, exponent) self._tail = self._head def __str__(self) -> str: """Return a string representation of this polynomial. # Polynomial with no terms: >>> p = Polynomial() >>> str(p) '' # Polynomial with one term (a constant): >>> p = Polynomial(12) >>> str(p) '12' # Polynomials with one term: >>> p = Polynomial(12, 1) >>> str(p) '12x' >>> p = Polynomial(12, 2) >>> str(p) '12x^2' # See __add__ for string representations of polynomials with # more than one term. """ s = '' node = self._head    while node is not None: s += str(node.coeff)    if len(s): s += '+'    if node.exp == 1: s += 'x' elif node.exp > 1: s += 'x^' + str(node.exp)    node = node.next    return s def __add__(self, rhs: 'Polynomial') -> 'Polynomial': """ Return a new Polynomial containing the sum of this polynomial and rhs. Raise ValueError if either polynomial has no terms. >>> p1 = Polynomial(12, 2) >>> p2 = Polynomial(-3, 1) >>> p3 = Polynomial(7) >>> p1 + p2 12x^2-3x >>> p1 + p3 12x^2+7 >>> p1 + p2 + p3 # Equivalent to (p1 + p2) + p3 12x^2-3x+7 >>> p2 = Polynomial(3, 1) >>> p1 + p2 + p3 12x^2+3x+7 """ poly = Polynomial.Termnode() node = self._head node2 = rhs._head    while node or node2 is not None: if node.exp == node2.exp: coeff = node.coeff + node2.coeff Please help me in implementing the __add__ function. Do not modify __str__. I am not getting how I am supposed to create and add a new node and add it to the empty Polynomial class. The incomplete function is below. Indentation dont work so only the first __init__ is inside TermNode. Only answer if you can.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

class Polynomial:

class TermNode:
def __init__(self, coefficient: int, exponent: int) -> None:
"""Initialize this node to represent a polynomial term with the
given coefficient and exponent.

Raise ValueError if the coefficent is 0 or if the exponent
is negative.
"""
if coefficient == 0:
raise ValueError("TermNode: zero coefficient")
if exponent < 0:
raise ValueError("TermNode: negative exponent")

self.coeff = coefficient
self.exp = exponent
self.next = None

def __init__(self, coefficient: int = None, exponent: int = 0) -> None:
"""Initialize this Polynomial with a single term constructed from the
coefficient and exponent.

If one argument is given, the term is a constant coefficient
(the exponent is 0).
If no arguments are given, the Polynomial has no terms.

# Polynomial with no terms:
>>> p = Polynomial()
>>> print(p._head)
None
>>> print(p._tail)
None

# Polynomial with one term (a constant):
>>> p = Polynomial(12)
>>> p._head.coeff
12
>>> p._head.exp
0
>>> print(p._head.next)
None

# Polynomial with one term:
>>> p = Polynomial(12, 2)
>>> p._head.coeff
12
>>> p._head.exp
2
>>> print(p._head.next)
None
"""
# A polynomial is stored as a singly linked list. Each node stores
# one term, with the nodes ordered in descending order, based on the
# exponent. (The head node is the term with the highest exponent,
# and the tail node is the term with the lowest exponent.)
if coefficient is None and exponent == 0:
self._head = None
else:
self._head = Polynomial.TermNode(coefficient, exponent)
self._tail = self._head

def __str__(self) -> str:
"""Return a string representation of this polynomial.

# Polynomial with no terms:
>>> p = Polynomial()
>>> str(p)
''

# Polynomial with one term (a constant):
>>> p = Polynomial(12)
>>> str(p)
'12'

# Polynomials with one term:
>>> p = Polynomial(12, 1)
>>> str(p)
'12x'

>>> p = Polynomial(12, 2)
>>> str(p)
'12x^2'

# See __add__ for string representations of polynomials with
# more than one term.
"""
s = ''
node = self._head
  
while node is not None:
s += str(node.coeff)
  
if len(s):
s += '+'
  
if node.exp == 1:
s += 'x'
elif node.exp > 1:
s += 'x^' + str(node.exp)
  
node = node.next
  
return s

def __add__(self, rhs: 'Polynomial') -> 'Polynomial':
""" Return a new Polynomial containing the sum of this polynomial
and rhs.

Raise ValueError if either polynomial has no terms.

>>> p1 = Polynomial(12, 2)
>>> p2 = Polynomial(-3, 1)
>>> p3 = Polynomial(7)
>>> p1 + p2
12x^2-3x

>>> p1 + p3
12x^2+7

>>> p1 + p2 + p3 # Equivalent to (p1 + p2) + p3
12x^2-3x+7

>>> p2 = Polynomial(3, 1)
>>> p1 + p2 + p3
12x^2+3x+7
"""
poly = Polynomial.Termnode()
node = self._head
node2 = rhs._head
  
while node or node2 is not None:
if node.exp == node2.exp:
coeff = node.coeff + node2.coeff

Please help me in implementing the __add__ function. Do not modify __str__. I am not getting how I am supposed to create and add a new node and add it to the empty Polynomial class. The incomplete function is below. Indentation dont work so only the first __init__ is inside TermNode. Only answer if you can.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education