Answer to Question #191713 in Python for Hari nadh babu

Question #191713

Add two polynomials


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


Output

Print the addition of polynomials A and B.

If the degree of polynomial is zero and the constant term is also zero, then just print 0 to represent the polynomial.

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


for Question and Test Cases, Inputs and Outputs See this below url link

https://drive.google.com/file/d/1dEzy0tHPt-dveknUMw8ml85IARaoLcsE/view?usp=sharing

The above url link contains Add two polynomials question and total 4 test cases, Inputs and Outputs


We need all test cases can be came when code was run. I want exact outputs for all test cases




1
Expert's answer
2021-05-11T23:18:29-0400
def readPolynomialFromUser():
    number_polynomials = int(input())
    P=[0 for i in range(number_polynomials)]
    for i in range(0,number_polynomials):
        index_p,index_c = input().split(' ')
        P[int(index_p)] = int(index_c)
    return P


def computePolynomialSum(A,B):
    if(len(A)>=len(B)):
        polynomials_sum=[0 for i in range(len(A))]
        for i in range(0,len(A)):
            if(i>=len(B)):
                B.append(0)
            polynomials_sum[i]=A[i]+B[i]
        return polynomials_sum


    return -1




A=readPolynomialFromUser()
B=readPolynomialFromUser()
polynomials_sum=computePolynomialSum(A,B)
if(polynomials_sum==-1):
    polynomials_sum=computePolynomialSum(B,A)
    
if(sum(polynomials_sum)!=0):
    for i in range(len(polynomials_sum)-1,0,-1):
        if polynomials_sum[i] != 0:
            if i!=1:
                if polynomials_sum[i]>0:
                    if i==len(polynomials_sum)-1:
                        if polynomials_sum[i]<0:
                            polynomials_sum[i]=-1*polynomials_sum[i]
                            print (f' - {polynomials_sum[i]}x^{i}',end='')
                        else:
                            if polynomials_sum[i]==1:
                                print (f'x^{i}',end='')
                            else:
                                print (f'{polynomials_sum[i]}x^{i}',end='')
                    else:
                        print (f' + {polynomials_sum[i]}x^{i}',end='')
                else:
                    if polynomials_sum[i]!=-1:
                        if polynomials_sum[i]<0:
                            polynomials_sum[i]=-1*polynomials_sum[i]
                            print (f' - {polynomials_sum[i]}x^{i}',end='')
                        else:
                            print (f'{polynomials_sum[i]}x^{i}',end='')
                    else:
                        print (f' - x^{i}',end='')
            else:
                if polynomials_sum[i]>0:
                    print (f' + {polynomials_sum[i]}x',end='')
                else:
                    if polynomials_sum[i]!=-1:
                        if polynomials_sum[i]<0:
                            polynomials_sum[i]=-1*polynomials_sum[i]
                            print (f' - {polynomials_sum[i]}x',end='')
                        else:
                            print (f'{polynomials_sum[i]}x',end='')
                    else:
                        print (f'x',end='')
    if polynomials_sum[0]<0:
        polynomials_sum[0]=-1*polynomials_sum[0]
        print (f' - {polynomials_sum[0]}',end='')
    else:
        print (f' + {polynomials_sum[0]}',end='')
else:
    print(str(polynomials_sum[0]))
    




Tests:


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

Assignment Expert
12.05.21, 14:02

Dear Hari nadh babu, You're welcome. We are glad to be helpful. If you liked our service please press like-button beside answer field. Thank you!

Hari nadh babu
12.05.21, 13:22

Excellent & Awesome. Thank you !

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS