Answer to Question #175894 in Python for jayanth

Question #175894
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.
Output

Explanation

If M = 4 and for polynomial A
For power 0, co-efficient is 5
For power 1, co-efficient is 0
For power 2, co-efficient is 10
For power 3, co-efficient is 6.

If N = 3 and for polynomial B
For power 0, co-efficient is 1
For power 1, co-efficient is 2
For power 2, co-efficient is 4.
Then polynomial A represents "6x^3 + 10x^2 + 5", the polynomial B represents "4x^2 + 2x + 1" and the addition of A and B is "6x^3 + 14x^2 + 2x + 6"






1
Expert's answer
2021-03-26T19:18:24-0400
# Add polynomials

def read_polynom():
    n = int(input())
    p = {}
    for i in range(n):
        L = input().split()
        Pi = int(L[0])
        Ci = int(L[1])
        p[Pi] = Ci
    return p


def add_polynomilas(p, q):
    r = {}
    Pis = set(p).union(q)
    for Pi in Pis:
        if Pi in p:
            Ci = p[Pi]
        else:
            Ci = 0.0
        if Pi in q:
            Ci += q[Pi]
        if Ci != 0.0:
            r[Pi] = Ci
    return r

def tostring_polynom(p):
    res = ''
    first = True
    for Pi in sorted(p, reverse=True):
        Ci = p[Pi]
        if first:
            if Ci == 0 and Pi == 0:
                return '0'
            
            if Ci == 1 and Pi != 0:
                pass
            elif Ci == -1 and Pi != 0:
                res = '-'
            else:           
                res = f'{Ci}'
            first = False
            
        else:        
            if Ci > 0:
                res += ' + '
            elif Ci < 0:
                res += ' - '
            else:
                continue
            res += f'{abs(Ci)}'
        
        if Pi == 0:
            continue
        if Pi == 1:
            res += 'x'
        else:
            res += f'x^{Pi}'
    return res

def main():
    p = read_polynom()
    q = read_polynom()
    r = add_polynomilas(p, q)
    print(f'Add {tostring_polynom(p)} to {tostring_polynom(q)}')
    print(f'results {tostring_polynom(r)}')

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

Assignment Expert
13.05.21, 09:25

Dear chandu, please post a new question

chandu
13.05.21, 03:59

Tried using this code but failed in some test cases. a) 4 0 5 1 0 2 10 3 6 4 0 -5 1 0 2 -10 3 -6 EXPECTED OUTPUT:0 b) 7 0 2 1 3 2 1 5 0 3 6 4 7 6 -9 5 0 1 2 4 3 0 1 0 4 -5 EXPECTED OUTPUT:-9x^6 + 2x^4 + 6x^3 + 5x^2 + 3x + 3

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS