Iterators and generators · 15 / 15 Exercise Medium

Generator pipeline

Objective

Create a Python program that chains several generators, so that each value goes through all the stages before moving on to the next one.

Instructions

  • Read a line with integers separated by spaces.
  • Define def evens(numbers), which yields only the even numbers, and def squares(numbers), which yields the square of each number.
  • Chain them: list(squares(evens(numbers))).
  • Show Results: followed by the values, separated by spaces (print("Results:", *results)) and, on the next line, Sum: S.
Input
1 2 3 4 5 6
Output
Results: 4 16 36
Sum: 56

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise