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.
Please help me for this i am not able to do this
# 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])
Comments
Leave a comment