Iterated Application X55698


Statement
 

pdf   zip   main.py

html

The n-th iterated application of a single-argument function f is the function that, applied to value x, returns fn(x), that is, f(f(...f(x)...)), where f is applied n times. Zero applications of f correspond to the identity function (as usual, zero repetitions of anything correspond to a neutral element). Write a function iterated_appl(f, n) that returns the n-th iterated application of f. The value of n will be a non-negative integer, and f is expected to take floats to floats.

Sample session
>>> iterated_appl(lambda x: x*x, 3)(2)
256.0
>>> sqr = lambda x: x*x
>>> iterated_appl(sqr, 2)(3)
81.0
>>> iterated_appl(sqr, 0)(2)
2.0
>>> add_1 = lambda x: 1+x
>>> iterated_appl(add_1, 5)(3.1)
8.1
>>> iterated_appl(iterated_appl(add_1, 5), 2)(2)
12.0
Information
Author
Jordi Delgado \& José Luis Balcázar
Language
English
Official solutions
Python
User solutions
Python