Answer to Question #206213 in Python for Ayushi

Question #206213

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.



input:

4

0 5

1 0

2 10

3 6

3

0 1

1 2

2 4


output:

 File "main.py", line 12

  for j in range(m);

           ^

SyntaxError: invalid syntax


expected output:

6x^3 + 14x^2 + 2x + 6



please rectify it!!

i am using python 3.9




1
Expert's answer
2021-06-14T07:12:03-0400
def read_polynom():
    m = int(input())
    Ps = []
    Cs = []
    for _ in range(m):
        s = input()
        Pi = int(s.split()[0])
        Ci = int(s.split()[1])
        Ps.append(Pi)
        Cs.append(Ci)


    # format a string
    indx = [i[0] for i in sorted(enumerate(Ps), reverse=True, key=lambda x:x[1])]
    Ps = [Ps[i] for i in indx]
    Cs = [Cs[i] for i in indx]
    return Ps, Cs


def add_polynoms(A, B):
    PsA, CsA = A
    iA = 0
    PsB, CsB = B
    iB = 0
    PsC = []
    CsC = []


    while iA < len(PsA) and iB < len(PsB):
        if PsA[iA] == PsB[iB]:
            Ci = CsA[iA] + CsB[iB]
            if Ci:
                PsC.append(PsA[iA])
                CsC.append(Ci)
            iA += 1
            iB += 1
        elif PsA[iA] > PsB[iB]:
            if CsA[iA]:
                PsC.append(PsA[iA])
                CsC.append(CsA[iA])
            iA += 1
        else:
            if CsB[iB]:
                PsC.append(PsB[iB])
                CsC.append(CsA[iB])
            iB += 1
    
    while iA < len(PsA):
        if CsA[iA]:
            PsC.append(PsA[iA])
            CsC.append(CsA[iA])
        iA += 1


    while iB < len(PsB):
        if CsB[iB]:
            PsC.append(PsB[iB])
            CsC.append(CsB[iB])
        iB += 1
    
    if len(PsC) == 0:
        PsC = [0]
        CsC = [0]
    
    return PsC, CsC



def print_polynom(A):
    s = ''
    Ps, Cs = A
    for (Pi, Ci) in zip(Ps, Cs):
        if len(s) == 0 :
            if Ci == 0 and Pi == 0:
                s = '0'
                break
                
            if Ci == 0:
                continue
            if Ci == 1:
                pass
            elif Ci == -1:
                s = '-'
            else:           
                s = str(Ci)
            
        else:        
            if Ci > 0:
                s += ' + '
            elif Ci < 0:
                s += ' - '
            else:
                continue
                
            if abs(Ci) != 1:
                s += str(abs(Ci))
        
        if Pi == 0:
            if abs(Ci) == 1:
                s += str(abs(Ci))
        else:
            s += 'x'
            if Pi != 1:
                s += '^' + str(Pi)
                
    print(s)


def main():
    A = read_polynom()
    B = read_polynom()
    C = add_polynoms(A, B)
    print_polynom(C)


if __name__ == '__main__':
    main()

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