Polynomial
Given polynomial, write a program that prints polynomial in Cix^Pi + Ci-1x^Pi-1 +....+C1x+C0 format
Sample Input 1
5
0 2
1 3
2 1
4 7
3 6
Sample Input 1
7x^4 + 6x^3 + x^2 + 3x + 2
n = int(input())
# input tuples of Pi and Ci
PiCi = []
for i in range(n):
l = input().split()
Pi = int(l[0])
Ci = int(l[1])
PiCi.append((Pi, Ci))
PiCi.sort(reverse=True)
first = True
for Pi, Ci in PiCi:
# print Coefficent
if Ci == -1:
print(' - ', end='')
elif Ci == 1:
print(' + ', end='')
if Ci < 0.0:
if first:
print(Ci, end='')
else:
print(' -', abs(Ci), end='')
else:
if first:
print(Ci, end='')
else:
print(' +', abs(Ci), end='')
first = False
# print x^Pi
if Pi == 0:
continue
if Pi == 1:
print(f'x',end='')
continue
if Pi < 0:
print(f'x^({Pi})', end='')
else:
print(f'x^{Pi}', end='')
print()
Comments
Leave a comment