Answer to Question #253427 in Python for Pooja

Question #253427

given polynomial write a program that prints polynomial in cix^pi + ci-1x^pi-1 c1x + c0 format


1
Expert's answer
2021-10-19T05:02:40-0400
# We get the coefficients of the polynomial
pol = {}
n = int(input('Enter the number of terms of the polynomial'))
for _ in range(n):
    c = float(input('Enter the coefficient of the polynomial term'))
    p = int(input('Enter the power  of the polynomial term'))
    # if the power of the polynomial term has already been introduced,
    # we sum up the coefficients
    if p in pol:
        pol[p] += c
    else:
        pol[p] = c
# Polynomial printing
for i in sorted(pol, reverse=True):
    if i == 0:
        print(f'{pol[i]:+}', end='')
    elif i == 1:
        print(f'{pol[i]:+}x', end='')
    else:
        print(f'{pol[i]:+}x^{i}', end='')

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