Write a function that takes another function as an argument.
What to build
A function apply_twice(f, x).
Requirements
apply_twice(f, x)returnsf(f(x)).- The starter uses it with two different functions. Do not change those lines.
Input
One whole number.
Output
Two lines: the number doubled twice, then the number plus 3, twice.
Constraints
The number is from -1000 to 1000.
Example
Input:
5
Output:
20
11
Why it works
apply_twice does not need to know what f does. That is what makes passing functions so flexible.
Examples
Example 1 · Input
5
Expected output
20 11
Example 2 · Input
0
Expected output
0 6
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Call f on x, then call f on that result.
Hint 2
Pass a function by writing its name with no brackets.
Solution
Show one correct solution (try it yourself first)
def apply_twice(f, x):
return f(f(x))
def double(n):
return n * 2
n = int(input())
print(apply_twice(double, n))
print(apply_twice(lambda v: v + 3, n))