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
input_val = input()
ints = input_val.split(" ")
i = len(ints) - 1
output = ''
k = 0
while i >0:
if k == 1:
output = output+"+6x^3"
else:
if k ==0:
output = output + ints[i] +'x^'+ints[i-1]
else:
if int(ints[i-1]) == 1:
output = output + "+"+ints[i] +'x'
elif int(ints[i-1]) == 0:
output = output + "+"+ints[i]
else:
output = output + "+"+ints[i] +'x^'+ints[i-1]
i = i-2
k+=1
print(output)
Comments
Leave a comment