Answer to Question #233981 in Python for Lovely

Question #233981

Not getting output please send corrected output

Add two polynomials


Given two polynomials A and B, write a program that adds the given two polynomials A and B.


Input


The first line contains a single integer M. Next M lines contain two integers Pi, Ci separated with space, where Pi denotes power and Ci denotes co-efficient of Pi for polynomial A. After that next line contains a single integer N. Next N lines contain two integers Pj, Cj separated with space, where Pj denotes power and Cj denotes co-efficient of Pj for polynomial B.


1
Expert's answer
2021-09-09T11:03:11-0400
M = int(input())
polynomialA=[0 for i in range(M)]
for i in range(0,M):
    Pj,Cj = input().split(' ')
    polynomialA[int(Pj)] = int(Cj)




N = int(input())
polynomialB=[0 for i in range(N)]
for i in range(0,N):
    Pj,Cj = input().split(' ')
    polynomialB[int(Pj)] = int(Cj)


if(len(polynomialB)<=len(polynomialA)):
    pSumResult=[0 for i in range(len(polynomialA))]
    for i in range(0,len(polynomialA)):
        if(i>=len(polynomialB)):
            polynomialB.append(0)
        pSumResult[i]=polynomialA[i]+polynomialB[i]
else:
    pSumResult=None


if(pSumResult==None):
    if(len(polynomialA)<=len(polynomialB)):
        pSumResult=[0 for i in range(len(polynomialB))]
        for i in range(0,len(polynomialB)):
            if(i>=len(polynomialA)):
                polynomialA.append(0)
        pSumResult[i]=polynomialB[i]+polynomialA[i]
    
if(sum(pSumResult)!=0):
    for i in range(len(pSumResult)-1,0,-1):
        if pSumResult[i] != 0:
            if i!=1:
                if pSumResult[i]>0:
                    if i==len(pSumResult)-1:
                        if pSumResult[i]<0:
                            pSumResult[i]=-1*pSumResult[i]
                            print (f' - {pSumResult[i]}x^{i}',end='')
                        else:
                            if pSumResult[i]==1:
                                print (f'x^{i}',end='')
                            else:
                                print (f'{pSumResult[i]}x^{i}',end='')
                    else:
                        print (f' + {pSumResult[i]}x^{i}',end='')
                else:
                    if pSumResult[i]!=-1:
                        if pSumResult[i]<0:
                            pSumResult[i]=-1*pSumResult[i]
                            print (f' - {pSumResult[i]}x^{i}',end='')
                        else:
                            print (f'{pSumResult[i]}x^{i}',end='')
                    else:
                        print (f' - x^{i}',end='')
            else:
                if pSumResult[i]>0:
                    print (f' + {pSumResult[i]}x',end='')
                else:
                    if pSumResult[i]!=-1:
                        if pSumResult[i]<0:
                            pSumResult[i]=-1*pSumResult[i]
                            print (f' - {pSumResult[i]}x',end='')
                        else:
                            print (f'{pSumResult[i]}x',end='')
                    else:
                        if pSumResult[i]<0:
                            print (f' - x',end='')
                        else:
                            print (f' + x',end='')
    if pSumResult[0]<0:
        pSumResult[0]=-1*pSumResult[0]
        print (f' - {pSumResult[0]}',end='')
    else:
        print (f' + {pSumResult[0]}',end='')
else:
    print(f'{pSumResult[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