添加两个多项式 Python 程序

在此代码中有四个测试用例。第一个、第二个和第四个测试用例成功完成,但第三个测试用例没有得到预期的输出。请帮助我,我在第三个测试用例中得到 12x^4 + 9x^3 - 5x^2x - 1 作为输出,但预期输出是 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]))
stack overflow Add Two Polynomials Python Program
原文答案
author avatar

接受的答案

您的问题发生在:

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

您应该将代码更改为 print (f' - x',end='')

此外,您使用的选项卡有点太多,我建议您重新排列代码,如:

# 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='')

这很清楚

第 5 个输入的新编辑:

因为你缺少判断系数<0条件下的第一项,所以代码应该是:

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='')

答案:

作者头像

这是正确的代码:

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

else:
    print(str(polynomials_sum[0]))

我亲自匹配了所有的输出,它们都是正确的。

之前出了什么问题:

  • 没有考虑 0th 多项式项
  • 对于多项式的最后一项, x^0 被打印出来。
  • 对于多项式的倒数第二项,正在打印 x^1 ,应该是 x