given polynomial write a program that prints polynomial in cix^pi + ci-1x^pi-1 c1x + c0 format
# 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='')
Comments
Leave a comment