Computers and Technology

I am trying to implement __add__ but I am having trouble what to write next. How can it be implemented? Realted methods are given along with __add__: from typing import Any

__author__ = 'bailey'
__version__ = '1.00'
__date__ = 'February 21, 2022'

"""
History:
1.00 Feb. 21, 2022 - Initial release.
"""

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

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 04:30, safi30360oz0c34
What kind of software users of all skill levels create web pages that include graphics, video, audio, animation, and other special effects? website authoring website software website publishing website editing
Answers: 1
image
Computers and Technology, 24.06.2019 14:30, yeet74
Ahousehold consists of a married couple and their twin five-year old daughters. the couples children had no income and lived with their parents all of last year. how many exemptions can the couple claim on last years tax return or they file with the “ married filing jointly “ status? a. 4 b. 5 c. 3 d. 2
Answers: 1
image
Computers and Technology, 24.06.2019 17:00, rosepetals2938
Carlos, an algebra teacher, is creating a series of powerpoint presentations to use during class lectures. after writing, formatting, and stylizing the first presentation, he would like to begin writing the next presentation. he plans to insert all-new content, but he wants to have the same formatting and style as in the first one. what would be the most efficient way for carlos to begin creating the new presentation? going under the file tab and opening the first presentation, deleting all content from each page, and adding new content going under the file tab and clicking on new in the left pane, then choosing new from existing going under the design tab and clicking on themes, then selecting the theme that was used for the first template going under the design tab and opening the template that was created for the first presentation
Answers: 2
image
Computers and Technology, 24.06.2019 18:00, janeou17xn
Which of the following is an example of synchronous communication? a) e-mail b) voicemail c) telephone conversation d) text message.
Answers: 1
Do you know the correct answer?
I am trying to implement __add__ but I am having trouble what to write next. How can it be implement...

Questions in other subjects:

Konu
English, 20.11.2020 17:50