Data & Features · Easy
Minibatch Iterator
Implement Minibatch Iterator with production-minded edge case handling.
Task
Implement minibatch_iterator(values: list[float], batch_size: int, drop_last: bool) -> list[list[float]]. Split a one-dimensional sequence into consecutive minibatches while preserving input order.
Requirements
- batch_size is a positive integer.
- Preserve the original order of all retained values.
- Every batch except a possible final batch has exactly batch_size elements.
- When drop_last is False, include a shorter final batch if values do not divide evenly.
- When drop_last is True, discard an incomplete final batch.
- Return an empty list for empty input, and do not mutate values.
Example
minibatch_iterator([10.0, 20.0, 30.0, 40.0, 50.0], 2, False)
# [[10.0, 20.0], [30.0, 40.0], [50.0]]