Answer to Question #305937 in Python for Prashanth

Question #305937

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

1
Expert's answer
2022-03-04T08:34:23-0500
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS