Editor

Apply Twice

#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.

Examples

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.

Note

func is called exactly twice: first with x, then with what the first call returned.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.