Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds
consecutive_groups()
, a tool for finding consecutive blocks of items in an iterable. This is an extension of the example in the old itertools docs.I've always admired the trick offered in the example, which is excerpted here:
This works because
enumerate
counts up by 1 for each item in the iterable (0, 1, 2, 3...
), so if we have a run of numbers (20, 21, 22, 23
), we'll wind up with a constant difference between the index and each item in the run (20 - 0 = 20
,21 - 1 = 20
,22 - 2 = 20
, ...). This we use for grouping.The example shows how to work with iterables of numbers, but the same method can be applied to any sequence with a clear ordering. I've used this for finding consecutive IP addresses, for example.
The tests show another example where there's not an obvious numerical representation, but all we need is a ranking method.