Build a Text Transform Pipeline From a List of Functions

Hard ⏱ 18 min 26% acceptance ★★★★☆ 4.4
Write a function run_pipeline(text, steps) where steps is a list of single-argument functions, each taking a string and returning a string. Apply each function in steps to text in order (the output of one step becomes the input to the next) and return the final string. Do not use recursion; use an ordinary loop.

Examples

Example 1
Input
steps = [str.strip, str.lower, lambda s: s.replace(" ", "_")]
run_pipeline("  Hello World  ", steps)
Output
hello_world
Explanation

strip removes outer spaces, lower lowercases, then spaces are replaced with underscores, applied in sequence.

Example 2
Input
run_pipeline("abc", [])
Output
abc
Explanation

No steps means the text passes through unchanged.

Constraints

  • steps may be empty.
  • Each function in steps takes and returns a string.

Topics

Functionshigher-order functions

Companies

AdobeLinkedInAtlassian

Hints

Hint 1

Loop over steps, reassigning text = step(text) each time.

Hint 2

An empty steps list means the loop body never executes, so the original text is returned.

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