GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Polynomial operation in python

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.

Hint: You are not restricted to writing just the two functions asked for.  You can write auxiliary functions to "clean up" polynomials --- e.g., remove zero coefficient terms, combine like terms, sort by exponent etc.  Build a library of functions that can be combined to achieve the desired format.

You may also want to convert the list representation to a dictionary representation and manipulate the dictionary representation, and then convert back.

Some examples:

>>> addpoly([(4,3),(3,0)],[(-4,3),(2,1)])
[(2, 1),(3, 0)]

Explanation: (4x^3 + 3) + (-4x^3 + 2x) = 2x + 3

>>> addpoly([(2,1)],[(-2,1)])
[]

Explanation: 2x + (-2x) = 0

>>> multpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)])
[(1, 3),(-1, 0)]

Explanation: (x - 1) * (x^2 + x + 1) = x^3 - 1

asked Aug 22, 2016 by anonymous
...