0
Why isn't this working?
class Polynomial(): """"Polynomial that takes a list of the coefficients of a polynomial in its initializer""" def __init__(self,coeffs1,coeffs2,coeffs3,coeffs4,coeffs5,coeffs6,coeffs7): self.coeffs1 = coeffs1 self.coeffs2 = coeffs2 self.coeffs3 = coeffs3 self.coeffs4 = coeffs4 self.coeffs5 = coeffs5 self.coeffs6 = coeffs6 self.coeffs7 = coeffs7 def value(*coeffs,x): n = len(coeffs) highest_power = n - 1 max_index = highest_power v = 0 for i in range(max_index+1): v += coeffs[i]*x**(n-i-1) return v p = Polynomial(3, 0,0,0,0,-5,10) p.value(1)
3 Respostas
+ 1
nai
What if there is more than 10 values? So for every value doing self.coeff is not worth so what you can do. You can initialise like this:
def __init__(self, *coeff):
self.coeff = list(coeff)
And also your defined function "value" should have 1st parameter as self.
And 3rd thing
max_index + 1 is equal to n so you can directly write range(n)
max_index + 1 = n?
max_index + 1 = highest_power + 1 = n - 1 + 1 = n
Now here is your solution:
https://code.sololearn.com/c3KNAiAWZG8u/?ref=app
0
if both function are in a class definition, then the second one should be either a static method, or have a first 'self' argument ;P