codejump
HardPro challengePythonJavaScriptTypeScript

Generator Pipeline — Multi-Stage

PythonGeneratorsFunctional

Build a lazy three-stage pipeline using generators:

1. read_numbers(data) — yields each integer from data (a list).
2. square_odds(source) — consumes the previous generator, yields the square of each odd number.
3. take(source, n) — yields the first n items from source.

Implement solve(data, n) that chains the three generators and returns the results as a list.

Examples

  • solve([1,2,3,4,5,6,7], 3) → [1, 9, 25]
  • solve([2,4,6], 3) → [] *(no odds)*
  • solve([1,3,5,7,9], 2) → [1, 9]

In JavaScript / TypeScript — Use generator functions (function*): readNumbers, squareOdds and take. take must stop pulling from its source once it has yielded n items.

Sample tests

Test #1Mixed, take 3 odd-squares
Input: [[1,2,3,4,5,6,7],3]
Output: [1,9,25]
Test #2No odds — empty result
Input: [[2,4,6],3]
Output: []
Test #3All odds, take 2
Input: [[1,3,5,7,9],2]
Output: [1,9]