Answer to Question #226611 in Python for MANVITHA

Question #226611


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

I/p

The 1st line contains a single integer N.

Next N lines contain 2 integers Pi, Ci separated with space, Pi denotes power and Ci denotes coef of Pi.

O/p

Print poly in format Cix^Pi + Ci-1x^Pi-1 + .. + C1x + C0, Pi's r powers in dec order, Ci is coeff, & C0 is constant. There will be space before & after + or - sign.

If coeff is 0, don't print term.

If term with highest degree is -ve, term should represent -Cix^Pi.

For term power is 1, represent it as C1x instead of C1x^1.

If the poly degree is 0 & constant term is also 0, then print 0 to represent the poly.

For term Cix^Pi, if coef of term Ci is 1, print x^Pi instead of 1x^Pi.

Exp:

If N = 4

For power 0, the coef is 5

1, the coef is 0

2, the coef is 10

3, the coef is 6.

Then poly n represents "6x^3 + 10x^2 + 5"Constraints

N <= 100

0 <= Pi < 1000

-1000 <= Ci <= 1000

Sample I/p

4

0 5

1 0

2 10

3 6

Sample O/p

6x^3 + 10x^2 + 5




1
Expert's answer
2021-08-16T14:49:54-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__':
   ​prin

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