Answer to Question #222659 in Python for krish

Question #222659

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


Print the addition of polynomials A and B.

The format for printing polynomials is: Cix^Pi + Ci-1x^Pi-1 + .... + C1x + C0, where Pi's are powers in decreasing order, Ci is co-efficient and C0 is constant, there will be space before and after the plus or minus sign.




Input 2

4

0 5

1 0

2 10

3 6

4

0 -5

1 0

2 -10

3 -6


output





1
Expert's answer
2021-08-02T15:55:24-0400
# enter dataset polinom
print('Plolinom A data entry')
n = int(input('Enter the number of N polynomial members '))
polinom_a =[0 for item in range(n)]
for item in range(n):
    p, c = input('Enter separated by space Pi and Ci ').split(' ')
    polinom_a[int(p)] = int(c)
m = int(input('Enter the number of M polynomial members '))
polinom_b =[0 for item in range(m)]
print('Plolinom B data entry')
for item in range(m):
    p, c = input('Enter separated by space Pi and Ci ').split(' ')
    polinom_b[int(p)] = int(c)
# define references to polynomials of greater and lesser length
pol_max , pol_min = polinom_a , polinom_b
if len(pol_min) > len(pol_max):
    pol_max , pol_min = polinom_b , polinom_a 
# add the coefficients of the polynorms in pairs
for item in range(len(pol_min)):
    pol_max[item] += pol_min[item]
# result output
for item in range(len(pol_max)-1,0,-1):
    if item == 0:
        continue
    print (f'{pol_max[item]}x^{item} + ',end='')
print(pol_max[0])

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