diff options
author | Joris | 2020-05-08 14:12:47 +0200 |
---|---|---|
committer | Joris | 2020-05-08 14:14:16 +0200 |
commit | df828c4b141f84f731afffbe17c80618cacf9480 (patch) | |
tree | aeae4a185c3f23da0331b32139c072ebe50b50dd /src/util | |
parent | 2abfaddc6a3a5233018ad285421eaf531a01a283 (diff) |
Bootstrap todo-next
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/array.py | 5 | ||||
-rw-r--r-- | src/util/range.py | 30 |
2 files changed, 35 insertions, 0 deletions
diff --git a/src/util/array.py b/src/util/array.py new file mode 100644 index 0000000..7dd0357 --- /dev/null +++ b/src/util/array.py @@ -0,0 +1,5 @@ +def insert_position(x, xs, is_reversed: bool) -> int: + for i, y in enumerate(xs): + if is_reversed and x > y or not is_reversed and x < y: + return i + return len(xs) diff --git a/src/util/range.py b/src/util/range.py new file mode 100644 index 0000000..c75232c --- /dev/null +++ b/src/util/range.py @@ -0,0 +1,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 |