Sunday, December 4, 2016

Python: Returning Polynomial Function

Command:

$ cat makePoly.py 


Result:

# ax + b
def makePoly(arr):
    def fn(x):
        return sum(c*x**p for p,c in enumerate(arr))
    return fn

my_func = makePoly([6, 2])
print my_func(3) #return 12


Command:

$ python makePoly.py 


Result:

12