Answer to Question #242423 in Python for srinath

Question #242423
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
1
Expert's answer
2021-09-26T11:42:02-0400
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)

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