-1

I wanna create a function to generate a polynomial with corresponding coefficients.

class Polynomial:
    def __init__(self, *coefficients):
        self.coefficients = coefficients
    def __len__(self):
        return len(self.coefficients)
    def __repr__(self):
        equation = ""
        for i in range(len(self)):
            if i == 0:
                equation += "(" + str(self.coefficients[i]) + "x^" + str(i)
            else:
                equation += "+" + str(self.coefficients[i]) + "x^" + str(i)
            return equation + ")"
    def __lt__(self, other):
        return self.coefficients < other.coefficients
    def __ge__(self, other):
        return self.coefficients >= other.coefficients 
a = Polynomial(1,2,3)
print(a)

I expected '(1x^0+2x^1+3x^2)' to be printed, but It was just (1x^0). What would be the problem in my code? thank you in advance.

Lonnie Kim
  • 11
  • 4

4 Answers4

0

In your repr method you return is over indented. I would also suggest storing your coefficients as a list for easy construction with any iterable.

def __init__(self, coefficients):
    self.coefficients = list(coefficients)
Noah Smith
  • 187
  • 4
0

You are returning [[equation + ")"]] from wrong indent. The loop is only running for one iteration i = 0. YOUR CODE:

for i in range(len(self)):
            if i == 0:
                equation += "(" + str(self.coefficients[i]) + "x^" + str(i)
            else:
                equation += "+" + str(self.coefficients[i]) + "x^" + str(i)
------->    return equation + ")"

INSTEAD DO:

for i in range(len(self)):
            if i == 0:
                equation += "(" + str(self.coefficients[i]) + "x^" + str(i)
            else:
                equation += "+" + str(self.coefficients[i]) + "x^" + str(i)
return equation + ")"
sage07
  • 83
  • 8
0

As others have already pointed out, the problem with your repper was that the return statement was at the wrong level of indentation.

An arguably deeper, more important problem is that your intended repper does not allow to recreate an indentical object with eval(repr(my_poly)) - in your example that would be eval('Polynomial(1, 2, 3)')

Mentioned in this answer:

... the purpose of __str__ is to be readable, whereas the purpose of __repr__ is to be unambiguous.

The following example uses your original (and corrected) repper as the __str__ method, and adds a repper that your peers will expect. The use of __class__ will allow your subclassers to be able to directly use it too.

class Polynomial:

    def __init__(self, *coefficients):
        self.coefficients = tuple(coefficients)

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

    def __str__(self):
        equation = ""
        for degree, factor in enumerate(self.coefficients):
            if degree == 0:
                equation += "(" + str(factor) + "x^" + str(degree)
            else:
                equation += " + " + str(factor) + "x^" + str(degree)
        return equation + ")"

    def __repr__(self):
        return f'{self.__class__}{tuple(self.coefficients)}'


my_poly = Polynomial(1, 2, 3)

print(my_poly)     # (1x^0 +2 x^1 + 3x^2)
print([my_poly])   # ['Polynomial(1, 2, 3)']
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
-1

The most prominent problem is with the return statement inside the for loop.

class Polynomial:
    def __init__(self, *coefficients):
        self.coefficients = coefficients
    def __len__(self):
        return len(self.coefficients)
    def __repr__(self):
        equation = "("
        for i in range(len(self)):
            if i != 0:
                equation += "+"
            equation += str(self.coefficients[i]) + "x^" + str(i)
        return equation + ")"
    def __lt__(self, other):
        return self.coefficients < other.coefficients
    def __ge__(self, other):
        return self.coefficients >= other.coefficients 
a = Polynomial(1,2,3)
print(a)
Tom Ron
  • 5,906
  • 3
  • 22
  • 38