Polynomial class - list assignment index out of range

Asked

Viewed 340 times

0

Save people, I’m learning classes in python and created one for application with polynomials, but I’m not able to add two distinct polynomials, always appears the following error:

File "/home/Documents/python/Poo/polinomio.py", line 30, in P3 = P1 + P2 File "/home/Leticia/Documents/python/Poo/polinomio.py", line 19, in add sum.terms[i] = self.terms[i] + other[i] Indexerror: list assignment index out of range

In these attempts to solve, I even added an attribute to pass the dimension of the vector, but I remain in it, where I am missing?

class Polinomio:

    def __init__ (self, termos = None, n = 0):
        self.termos = termos or []
        self.n = [0] * n

    def __len__ (self):
        return len(self.termos)

    def __setitem__ (self, i, x):
        self.termos[i] = x

    def __getitem__(self, i):
        return self.termos[i] 

    def __add__ (self, other):
        soma = Polinomio(n = self.termos.__len__())
        for i in range(self.termos.__len__()):
            soma.termos[i] = self.termos[i] + other[i]
        return soma

    def print (self):
        print(self.termos)


p1 = Polinomio([1, 2, 3])
p2 = Polinomio([1, 2, 3])
p2.print()
p3 = Polinomio()
p3 = p1 + p2
  • 1

    In the method init try to get self.termos = termos or [0]*n

  • Rode thank you if you want to answer the question so I can mark as resolved

  • You are using the "magic methods", in my opinion lacked an interesting good, for example, instead of doing p2.print() do print(p2) or print(p3) and see what happens. The desirable (or convention) would be that happen the same as what happens when you call the method print, that is, present the terms.

1 answer

1


[TL;DR]

Try it like this:

class Polinomio:

    def __init__ (self, termos = None, n = 0):
        self.termos = termos or []
        self.n = [0] * n

    def __len__ (self):
        return len(self.termos)

    def __setitem__ (self, i, x):
        self.termos[i] = x

    def __getitem__(self, i):
        return self.termos[i] 

    def __add__ (self, other):            
        return [x + y for x, y in zip(self.termos, other.termos)]

    def print (self):
        print(self.termos)

Testing:

p1 = Polinomio([1, 2, 3])
p2 = Polinomio([4, 5, 6])
print ('Termos de p1: ', p1.termos)
print ('Termos de p2: ', p2.termos)
print ('Soma de p1+p2: ', p1+p2)

Termos de p1:  [1, 2, 3]
Termos de p2:  [4, 5, 6]
Soma de p1+p2:  [5, 7, 9]

See working on repl.it.

  • All that was missing was the method add make a polynomial as well. And so the n is unnecessary, right?

  • To tell the truth I only paid attention to the mistake he made, nor did I analyze the problem as a whole. :-)

  • Yes, I imagined. In fact, using the zip there will be the problem of adding different order polynomials.

  • 1

    True! But if he has started I think it already helps (the logic of the problem is up to him, eheheh), sometimes I get amazed how beginners can not intrepretar the error messages.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.