Input: 5 0 2 1 3 2 1 4 7 Expected Output: 7x^4 + 6x^3 + x^2 + 3x + 2 What I got with the above code is : 7x^4 + 6x^3 + 1x^2 + 3 + 2
Then how to replace the 1x^2=x^2
Input = [0,2,1,3,2,1,4,7]
x=[]
Coef=[]
for r in range(0,len(Input)):
if(r%2==0): x.append(Input[r])
if(r%2==1): Coef.append(Input[r])
s=""
for r in range(0,len(x)):
if(x[r]==0):
s = s + str(Coef[r])+"+"
elif(x[r]==1):
if(Coef[r]==1):
s = s + "x"+"+"
else:
s = s + str(Coef[r]) + "x"+"+"
else:
if(Coef[r]>1):
s = s + str(Coef[r]) + "x^"+str(x[r])+"+"
else:
s = s + "x^"+str(x[r])+"+"
print("Polynomial: ",s)
Comments
Leave a comment