Skip to content
Advertisement

Add Two Polynomials Python Program

In this code there are four test cases. 1st and 2nd and 4th test cases are camed successfully but 3rd test case was not getting expected output. Please help me I got 12x^4 + 9x^3 – 5x^2x – 1 as output in 3rd test case but expected output is 12x^4 + 9x^3 – 5x^2 – x – 1

For Add two polynomials program question and 4 test cases Inputs and Outputs see below url

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

The above link contains Add two polynomials Question, 4 test cases and Inputs and Outputs

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: #redundent
                        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: #redundent
                        polynomials_sum[i]=-1*polynomials_sum[i]
                        if i==len(polynomials_sum)-1: #add p0 = i condition
                            print (f'-{polynomials_sum[i]}x^{i}',end='')
                        else:
                            print (f' - {polynomials_sum[i]}x^{i}',end='')
                    else:
                            if i==len(polynomials_sum)-1: #add p0 = i condition
                                print (f'-x^{i}',end='')
                            else:
                                print (f' - x^{i}',end='')
            else:
                if polynomials_sum[i]>0:
                    if i==len(polynomials_sum)-1: #add p0 = i condition
                        if polynomials_sum[i]==1:
                            print (f'x',end='')
                        else:
                            print (f'{polynomials_sum[i]}x',end='')
                    else:
                        if polynomials_sum[i]==1:
                            print (f' + x',end='')
                        else:
                            print (f' + {polynomials_sum[i]}x',end='')
                else:
                    #if polynomials_sum[i]<0: # redundent
                    polynomials_sum[i]=-1*polynomials_sum[i]
                    if i==len(polynomials_sum)-1: #add p0 = i condition
                        if polynomials_sum[i]==-1:
                            print (f'-x',end='')
                        else:
                            print (f'-{polynomials_sum[i]}x',end='')
                    else:
                        if polynomials_sum[i]==-1:
                            print (f' - x',end='')
                        else:
                            print (f' - {polynomials_sum[i]}x',end='')

    if polynomials_sum[0]<0:
        polynomials_sum[0]=-1*polynomials_sum[0]
        if len(polynomials_sum)-1 == 0: #add p0 = i condition
            print (f'-{polynomials_sum[0]}',end='')
        else:
            print (f' - {polynomials_sum[0]}',end='')
    else:
        if len(polynomials_sum)-1 == 0: #add p0 = i condition
            print (f'{polynomials_sum[0]}',end='')
        else:
            print (f' + {polynomials_sum[0]}',end='')
else:
    print(str(polynomials_sum[0]))

Advertisement

Answer

Your problem is occurred at:

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='') #here is your bug

you should change the code to print (f' - x',end='')

In addition, your use a little too much tab, I suggest you to rearrange the code like:

# if all element is 0, print "0" and end program
if polynomials_sum.count(0) == len(polynomials_sum): 
    print(0,end='')
    exit()

for i in range(len(polynomials_sum)-1,-1,-1):

    coff = polynomials_sum[i] #shorten the parameter

    # if the coefficient is 0, end this iteration
    if coff == 0: 
        continue # use continue to go next iteration immediately

    # if the coefficient is not 0 and term is 0, print directly and end iteration
    if i == 0: 
        print(f' + {coff}',end='') if coff > 0 else print(f' - {abs(coff)}',end='')
        continue
    
    # if the coefficient is not 0 and term is first term, hide the + if coff > 0 and end iteration
    # if p0 != 1, print term 
    if i == len(polynomials_sum)-1 and i != 1: 
        # if coff not 1/-1, print the coff, else just print +/-
        if abs(coff) != 1:
            print(f'{coff}x^{i}',end='') if coff > 0 else print(f'-{abs(coff)}x^{i}',end='')
        else:
            print(f'x^{i}',end='') if coff > 0 else print(f'-x^{i}',end='')
        continue

    # if p0 == 1, ignore term 
    if i == len(polynomials_sum)-1 and i == 1: 
        # if coff not 1/-1, print the coff, else just print +/-
        if abs(coff) != 1:
            print(f'{coff}x',end='') if coff > 0 else print(f'-{abs(coff)}x',end='')
        else:
            print(f'x',end='') if coff > 0 else print(f'-x',end='')
        continue

    # if the coefficient is not 0 and term is x^1 term, hide the Pi and end iteration
    if i == 1: 
        # if coff not 1/-1, print the coff, else just print +/-
        if abs(coff) != 1:
            print(f' + {coff}x',end='') if coff > 0 else print(f' - {abs(coff)}x',end='')
        else:
            print(f' + x',end='') if coff > 0 else print(f' - x',end='')
        continue

    # if coff not 1/-1, print the coff, else just print +/-
    if abs(coff) != 1:
        print(f' + {coff}x^{i}',end='') if coff > 0 else print(f' - {abs(coff)}x^{i}',end='')
    else:
        print(f' + x^{i}',end='') if coff > 0 else print(f' - x^{i}',end='')
        
            

this is much clearly

new edit for 5th input:

because your are lack of judging the first term in coefficient < 0 condition, so the code should be:

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: #these code is redundent
        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 i==len(polynomials_sum)-1:
        if polynomials_sum[i]!=-1:
            #if polynomials_sum[i]<0:
            #    polynomials_sum[i]=-1*polynomials_sum[i]
            #else:
            #    print (f'{polynomials_sum[i]}x^{i}',end='') #redundent code
            print (f'-{-polynomials_sum[i]}x^{i}',end='')
        else:
            print (f'-x^{i}',end='')
    else:
        if polynomials_sum[i]!=-1:
            print (f' - {-polynomials_sum[i]}x^{i}',end='')
        else:
            print (f' - x^{i}',end='')
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement