10 подписчиков
📑 Nested Functions for Polynomials
📑 Task
1) What will be the result of the calculations?
2) Can you write this code in a form of an anonymous function?
3) Can you make this function more universal so that
it builds a polynomial of any degree using arguments as coefficients?
def polynomials(a, b, c):
def polynomial(x):
return a * x**2 + b * x + c
return polynomial
polynomials(polynomials(1, 1, 1)(0), 1, 1)(0)
📑 Answer
1) 𝑝𝑜𝑙𝑦𝑛𝑜𝑚𝑖𝑎𝑙𝑠(1,1,1)(0) builds the function 𝑓(𝑥)=𝑥2+𝑥+1
and finds its value 𝑓(𝑥)=1 at 𝑥=0 twice
So the program will print out 1
2)
polynomials_lambda = \
lambda a, b, c: lambda x: a * x**2 + b * x + c
polynomials_lambda(polynomials_lambda(1, 1, 1)(0), 1, 1)(0)
Около минуты
23 апреля 2024