This is a task I was given for an online course. This is my solution to the problem.
Task:
Write a function called general_poly
, that meets the specifications below.
For example, general_poly([1, 2, 3, 4])(10)
should evaluate to 1234
because
1*10^3 + 2*10^2 + 3*10^1 + 4*10^0
So in the example the function only takes one argument with general_poly([1, 2, 3, 4])
and it returns a function that you can apply to a value, in this case x = 10
with general_poly([1, 2, 3, 4])(10)
.
def general_poly (L):
""" L, a list of numbers (n0, n1, n2, ... nk)
Returns a function, which when applied to a value x, returns the value
n0 * x^k + n1 * x^(k-1) + ... nk * x^0 """
#YOUR CODE HERE
My Code:
def general_poly (L):
""" L, a list of numbers (n0, n1, n2, ... nk)
Returns a function, which when applied to a value x, returns the value
n0 * x^k + n1 * x^(k-1) + ... nk * x^0 """
def function_generator(L, x):
k = len(L) - 1
sum = 0
for number in L:
sum += number * x ** k
k -= 1
return sum
def function(x, l=L):
return function_generator(l, x)
return function
L[i]
instead ofnumber
? \$\endgroup\$ – Arnial Oct 30 '16 at 12:24