Answer to Question #259979 in Python for Raju

Question #259979

Polynomial


Given polynomial,write a program that prints polynomial in Cix^Pi + Ci-1x^Pi-1+....+C1x+C0 format.


Input


The first line contains a single Integer N.

Next N lines contain two integers Pi,Ci separated with space, where Pi denotes power and Ci denotes coefficient of Pi.


Sample Input 1

5

0 2

1 3

2 1

4 7


Sample output 1

7x^4 + 6x^3 + x^2 + 3x + 2


Sample input 2

4

0 5

1 0

2 10

3 6


Sample output 2

6x^3 + 10x^2 + 5


1
Expert's answer
2021-11-01T18:57:13-0400
n = int(input())


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: ​ ​if Ci == 0: ​continue ​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 ​ ​if Pi == 0: ​continue ​if Pi == 1: ​continue ​if Pi < 0: ​print(f'x^({Pi})', end='') ​else: ​print(f'x^{Pi}', end='') 

if ​__name__ == '__main__'
 ​print

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