easy +8 pts

Generator Pipeline

Build a generator that lazily filters and maps an iterable of numbers.

Write a generator function named `pipeline` that takes a single argument `numbers`, which is an iterable of integers. When iterated, `pipeline(numbers)` must yield, in the same order they appear, the squares of only the even numbers in the input. The generator must be lazy: it should not process the entire input upfront, and it should work with any iterable (including infinite iterables like `itertools.count()`). You must not use `yield from`, list comprehensions, or generator expressions. Only plain `yield` statements and loops are allowed. Define the function exactly as: ```python def pipeline(numbers): ... ``` Examples: - pipeline([1, 2, 3, 4]) yields 4, 16. - pipeline([]) yields nothing. - pipeline([5, 7, 9]) yields nothing. - pipeline([2]) yields 4.

Constraints

Input numbers is an iterable of integers. The function returns a generator object. Each element can be any integer (positive, negative, or zero). There is no limit on the length of the iterable, but the generator must be lazy.

Example

>>> gen = pipeline([1, 2, 3, 4])
>>> list(gen)
[4, 16]
>>> list(pipeline([]))
[]
>>> list(pipeline([5, 7, 9]))
[]
>>> list(pipeline([2]))
[4]
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate over the input with a for loop.
Use the modulus operator `%` to test evenness.
Yield the square `n * n` only for even numbers.
Remember: generators are lazy, so you can just yield as you go.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.