#200 · Functions · Beginner · 25 XP · about 3 min
In Python a function is a value like any other: you can store it in a variable or hand it to another function as an argument.
Write apply_twice(func, x) that calls func with x, then calls func again with the result of that first call, and returns what the second call gives back.
apply_twice(lambda n: n + 3, 10) # returns 16 apply_twice(str.upper, 'hi') # returns 'HI' apply_twice(lambda s: s + '!', 'go') # returns 'go!!'
In the first example, 10 + 3 is 13, and 13 + 3 is 16.
func is called exactly twice: first with x, then with what the first call returned.
First solves only, ranked by stars and then time.