Tuesday, 20 September 2016

Add And Multiply Polynomials

Write Python functions for the following operations:

  addpoly(p1,p2)
 multpoly(p1,p2)
that add and multiply two polynomials, respectively.

You may assume that the inputs to these functions follow the 
representation given above.  Correspondingly, the outputs from these functions should also obey the same constraints.



def addpoly(p1,p2):
o=[]
if(len(p1)>len(p2)):
  h1=p2
  h2=p1
else:
  h1=p1
  h2=p2
for x in h1:
f=0
for y in h2:
if(x[1]==y[1]):
k=(y[0]+x[0],y[1])
f=1
break
if(f==0):
h2.append(x)


return h2[1:]

def multpoly(p1,p2):
l=[]
p=[]
if len(p1)>len(p2):
h2=p1
h1=p2
else:
h2=p2
h1=p1
for x in h1:
for y in h2:
k=(x[0]*y[0],y[1]+x[1])
l=add(l,k)

return l

4 comments:

  1. where is the definition of add function in multpoly function

    ReplyDelete
  2. multiplication of polynomial not working runtime error with
    'add' is not added....

    ReplyDelete