Answer to Question #244680 in Python for balu

Question #244680

my output:

12x^4 + 9x^3 - 5x^2 - 1x - 1


excepted output:

12x^4 + 9x^3 - 5x^2 - x - 1


For term Cix^Pi, if the coefficient of the term Ci is 1, simply print x^Pi instead of 1x^Pi.



1
Expert's answer
2021-10-01T01:31:55-0400
N = int(input())
A =[0 for item in range(N)]
for item in range(N):
    p, c = input().split(' ')
    A[int(p)] = int(c)
M = int(input())
B =[0 for item in range(M)]
for item in range(M):
    p, c = input().split(' ')
    B[int(p)] = int(c)
pol_max , pol_min = A , B
if len(pol_min) > len(pol_max):
    pol_max , pol_min = B , A
for item in range(len(pol_min)):
    pol_max[item] += pol_min[item]
for item in range(len(pol_max)-1,0,-1):
    if item == 0:
        continue
    if pol_max[item]==1 or pol_max[item]==-1 :
        print (f'x^{item} + ',end='')
    # elif pol_max[item]<0:
    #     print (f'{pol_max[item]}x^{item} ',end='')
    else:
        print (f'{pol_max[item]}x^{item} + ',end='')
print(pol_max[0])

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