← Back to challenges

Which Function Returns the Larger Number?

PythonHardhigher_order_functionslanguage_fundamentalsnumbers

Instructions

Your function will be passed two functions, f and g, that don't take any parameters. Your function has to call them, and return a string which indicates which function returned the larger number.

  • If f returns the larger number, return the string f.
  • If g returns the larger number, return the string g.
  • If the functions return the same number, return the string neither.

Examples

which_is_larger(lambda: 5, lambda: 10) ➞ "g"

which_is_larger(lambda: 25,  lambda: 25) ➞ "neither"

which_is_larger(lambda: 505050, lambda: 5050) ➞ "f"

Notes

This exercise is designed as an introduction to higher order functions (functions which use other functions to do their work).

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: A Long Long Time