EasyPro challengePython
Implement pipe()
PythonFunctionsDesign
Implement solve(fn_exprs, initial) where fn_exprs is a list oflambda 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) - 3 → 9
Rules
- Compile each expression string with
eval(expr). - If
fn_exprsis empty, returninitialunchanged.
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