Compose Two Transformations

Hard ⏱ 18 min 24% acceptance ★★★★☆ 4.2
Write compose(f, g) returning a new function equivalent to f(g(x)). Then use it to build clean = compose(str.strip, str.lower)... wait — order matters: compose(str.strip, str.lower) lowercases first, then strips. Your returned function must accept a single argument.

Examples

Example 1
Input
compose(lambda x: x + 1, lambda x: x * 2)(5)
Output
11
Explanation

g doubles 5 to 10, then f adds 1.

Example 2
Input
compose(str.strip, str.lower)('  HeLLo  ')
Output
'hello'
Explanation

Lowercased first, then stripped.

Constraints

  • Return a callable.
  • f runs on the result of g.

Topics

Lambda & Functionalfunction composition

Companies

MetaGoogleBloomberg

Hints

Hint 1

Return lambda x: f(g(x)).

Hint 2

Closures capture f and g automatically.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.