EasyPython

Implement pipe()

PythonFunctionsDesign

Implement solve(fn_exprs, initial) where fn_exprs is a list of
lambda expression strings and initial is the starting value.

Apply each function left-to-right, passing the result of each to the next
(like Unix pipes, or functools.reduce).

solve(['lambda x: x + 1', 'lambda x: x * 2', 'lambda x: x - 3'], 5)
((5 + 1) * 2) - 39

Rules

  • Compile each expression string with eval(expr).
  • If fn_exprs is empty, return initial unchanged.

Sample tests

Test #1Add then halve (integer division)
Input: [["lambda x: x + 10","lambda x: x // 2"],6]
Output: 8
Test #2Negate
Input: [["lambda x: -x"],7]
Output: -7
Test #3Single transform
Input: [["lambda x: x * x"],4]
Output: 16
Test #4Empty pipe returns initial
Input: [[],42]
Output: 42
Test #5Three transforms
Input: [["lambda x: x + 1","lambda x: x * 2","lambda x: x - 3"],5]
Output: 9