blob: c75232c06322162ff2c215f51220061d16737f1d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
from typing import NamedTuple
class Range(NamedTuple):
start: int
length: int
def from_indexes(indexes):
ranges = []
curr_range_start = 0
curr_range_len = 0
last_index = -1
for index in sorted(indexes):
if index == curr_range_start + curr_range_len:
curr_range_len += 1
else:
if curr_range_len > 0:
ranges.append(Range(
start = curr_range_start,
length = curr_range_len))
curr_range_start = index
curr_range_len = 1
if curr_range_len > 0:
ranges.append(Range(
start = curr_range_start,
length = curr_range_len))
return ranges
|