Derivative of a Polynomial

Given a polynomial expression (you can assume any representation for your convenience), return the derivative of the polynomial expression.

import collections


Poly = collections.namedtuple('Poly', 'ceof exp')

def derivative(poly):
    """
    :param poly: List[namedtuple] - polynomial expression
    :return: List[namedtuple] - derived polynomial expression
    """
    return [Poly(ceof=ceof*exp, exp=exp-1) for ceof, exp in poly if exp != 0]


t1 = [Poly(ceof=2, exp=3)]
print(derivative(t1))

t2 = [Poly(ceof=10, exp=1)]
print(derivative(t2))

t3 = [Poly(ceof=100, exp=0), Poly(ceof=2, exp=200)]
print(derivative(t3))

t4 = [Poly(ceof=100, exp=-3), Poly(ceof=2, exp=-30)]
print(derivative(t4))

Last updated