I'm trying to write a code and write test after this. But i have a problem. Only 1 functions gives the output. I didn't even get into the others. What is the reason? Why my programm ends before even 'visiting all the functions'. In my code I can't get into degree and coefficient. What is wrong with this code.
class Polynomia1:
def __init__(self, data=[]):
if len(data) > 0:
data = sorted(data, key=lambda x: x[0])
data.reverse()
b= []
for elem in data:
if elem[0] < 0:
raise ValueError("Negative power is not allowed")
if elem[0] >= 0:
b.append(elem[0])
if elem[1] == 0:
raise ValueError("coefficient cannot be empty")
if len(data) > len(set(b)):
raise ValueError("The value of power cannot be repeated")
self.data = data
def create_equation(self):
a = ""
if len(self.data) == 0:
a+= '0'
else:
for num in self.data:
if num[0] != 0 and num[0] != 1 and num[1] != 1 and num[1] != -1:
if len(a) == 0 or num[1] < 0:
a += f'{num[1]}x^{num[0]}'
else:
a += f'+{num[1]}x^{num[0]}'
if num[1] == -1 and num [0] != 0 and num[0] != 1:
a += f'-x^{num[0]}'
if num[1] == 1 and num [0] != 0 and num[0] != 1 and len(a) > 0:
a += f'+x^{num[0]}'
if num[1] == 1 and num [0] != 0 and num[0] != 1 and len(a) == 0:
a += f'x^{num[0]}'
if (len(a) == 0 or num[1] <=0)and num[0] == 0:
a += str(num[1])
if len(a) > 0 and num[1] > 0 and num[0] == 0:
a += f'+{num[1]}'
if ((len(a) == 0 or num[1] <=0) and num[1] != -1 and num[1]!=1) and num[0] == 1:
a += f'{num[1]}x'
if ((len(a) == 0 or num[1] <=0) and num[1] == -1 and num[1]!=1) and num[0] == 1:
a += f'-x'
if num[1] > 0 and num[1] != 1 and num [0] == 1 and len(a)>0:
a += f'+{num[1]}x'
if num[1] > 0 and (num[1] == 1 or num[1] == -1) and num [0] == 1 and len(a)>0:
a += f'+x'
if num[1] > 0 and (num[1] == 1 or num[1] == -1) and num [0] == 1 and len(a)==0:
a += f'x'
return a
def __degree__(self):
return max(self.data, key=lambda x:x[0])
def __str__(self):
"""Return information about polynomial"""
return self.create_equation()
def coefficient(self,number):
the_outcome = 0
for nuum in self.data:
if nuum[0] == number:
the_outcome = nuum[1]
return the_outcome
print(Polynomia1([(3, 5)]))