Question: Write a Python function make _ repeater, which requires exactly two positional arguments, which are as follows. The first positional argument is a repeatable function,

Write a Python function make_repeater, which requires exactly two positional arguments, which are as follows.
The first positional argument is a repeatable function, which is one that can be called with only one positional argument, and whose result could safely be passed back into the same function as an argument.
The second positional argument is a repeat count, a non-negative integer specifying how many times that function will be called.
The return value is a repeater function that itself requires one positional argument, an initial value, and calls the repeatable function the given number of times. The first time it's called, the initial value is passed to it; the second time it's called, the result of the first call is passed to it; the third time it's called, the result of the second call is passed to it; and so on. The result of the last call is the result returned by the repeater function.
Additionally, there are two performance requirements that must be met by your make_repeater function.
Your make_repeater function must require O(1) memory to run.
The repeater function returned by your make_repeater function must also require O(1) additional memory to run (i.e., independent of what the repeatable function does).
Here's an example of how you'd expect the function to behave when you're done with it.
>>> def square(n): ... return n * n ...>>> quadruple_square = make_repeater(square,4)>>> quadruple_square(2)65536 # square(square(square(square(2))))=65,536>>> single_square = make_repeater(square,1)>>> single_square(2)4 # square(2)=4>>> do_nothing = make_repeater(square,0)>>> do_nothing(2)2 # No calls to square, because the repeat count was 0.
Limitations
The Python standard library is entirely off-limits in this problem. Your function must not require any module to be imported.
What to submit
Submit one Python script named problem4.py, which contains your function and nothing else. Neither docstrings, comments, nor type annotations are required, since we've all agreed already on what problem we're solving here.
There is no credit being offered for writing automated tests though you certainly might want to, since that's a great way to ensure that your code works as you expect and we'd prefer you not submit them even if you do.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!