Skip to the workspace
Pebellslearn by building
← All interview tasks

Intermediate · Coding exercise

Consume an iterator in batches

generators · laziness · memory

Not checked yet

Guest practice stays in this visit. Sign in to save your progress. Checks cover the stated cases; discussion rubrics are self-reviewed. These tasks do not mark course lessons complete.

BuildConsume an iterator in batches

Implement batches(values, size) as a generator yielding lists of up to size items. Yield a final partial batch. Raise ValueError for size <= 0 when consumption begins. Do not read values until requested. Tests materialize output through collect_batches. Define that helper to return list(batches(values, size)).

Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.