Functions as Values
Completed
Write apply_n(f, x, n) that applies f to x n times. Read x and n, and print apply_n(double, x, n) and apply_n(increment, x, n).
Input
One line: x n.
Output
Two lines.
Example
Input
3 4
Output
48 7
Editor
Python
Hints
Functions are values: you can pass them as arguments without calling them.
Solution explanation
A higher-order function takes or returns other functions.
Show solution
def double(v):
return v * 2
def increment(v):
return v + 1
def apply_n(f, x, n):
for _ in range(n):
x = f(x)
return x
x, n = map(int, input().split())
print(apply_n(double, x, n))
print(apply_n(increment, x, n))