| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Algorithm functions.''' |
| 5 |
|
| 6 |
from collections import OrderedDict |
| 7 |
|
| 8 |
|
| 9 |
def flatten(iterable): |
| 10 |
'''Flatten a list of iterables into a single list.''' |
| 11 |
|
| 12 |
result = [] |
| 13 |
for item in iterable: |
| 14 |
result.extend(item) |
| 15 |
return result |
| 16 |
|
| 17 |
|
| 18 |
def uniq(iterable): |
| 19 |
'''Return list of unique items, preserving order.''' |
| 20 |
|
| 21 |
result = [] |
| 22 |
seen = set() |
| 23 |
for item in iterable: |
| 24 |
if item not in seen: |
| 25 |
seen.add(item) |
| 26 |
result.append(item) |
| 27 |
return result |
| 28 |
|
| 29 |
|
| 30 |
def partition(iterable, predicate): |
| 31 |
'''Split an iterable into two lists based on a predicate. |
| 32 |
|
| 33 |
Args: |
| 34 |
iterable: |
| 35 |
The input iterable to split. |
| 36 |
predicate (callable): |
| 37 |
A function returning True or False for each element. |
| 38 |
|
| 39 |
Returns: |
| 40 |
tuple: (matched, unmatched) where |
| 41 |
matched -- list of items for which predicate(item) is True |
| 42 |
unmatched -- list of items for which predicate(item) is False |
| 43 |
''' |
| 44 |
|
| 45 |
a, b = [], [] |
| 46 |
for item in iterable: |
| 47 |
if predicate(item): |
| 48 |
a.append(item) |
| 49 |
else: |
| 50 |
b.append(item) |
| 51 |
return (a, b) |
| 52 |
|
| 53 |
|
| 54 |
def group_by(iterable, keyfunc): |
| 55 |
'''Group items from an iterable by a key function. |
| 56 |
|
| 57 |
Args: |
| 58 |
iterable: |
| 59 |
The input iterable of items to group. |
| 60 |
keyfunc (callable): |
| 61 |
Function applied to each item to determine its group key. |
| 62 |
|
| 63 |
Returns: |
| 64 |
OrderedDict: |
| 65 |
A mapping of key -> list of items with that key, preserving |
| 66 |
the order of first appearance of each key. |
| 67 |
''' |
| 68 |
result = OrderedDict() |
| 69 |
|
| 70 |
for item in iterable: |
| 71 |
val = keyfunc(item) |
| 72 |
|
| 73 |
if val in result: |
| 74 |
result[val].append(item) |
| 75 |
else: |
| 76 |
result[val] = [item] |
| 77 |
|
| 78 |
return result |
| 79 |
|
| 80 |
|
| 81 |
def numbers_are_contiguous(numbers): |
| 82 |
'''Return True if the list of integers is contiguous (1, 2, 3, 4, ...)''' |
| 83 |
|
| 84 |
if not numbers: |
| 85 |
return False |
| 86 |
|
| 87 |
sorted_nums = sorted(numbers) |
| 88 |
return all(b - a == 1 for a, b in zip(sorted_nums, sorted_nums[1:])) |
| 89 |
|
| 90 |
|
| 91 |
class SparseList(list): |
| 92 |
'''A list that automatically grows when assigning past its end. |
| 93 |
|
| 94 |
Assigning to an index greater than or equal to the current length extends |
| 95 |
the list with the configured default value until the index exists. |
| 96 |
''' |
| 97 |
|
| 98 |
def __init__(self, iterable=(), default=None): |
| 99 |
super().__init__(iterable) |
| 100 |
self.default = default |
| 101 |
|
| 102 |
def _extend_to(self, index): |
| 103 |
if index >= len(self): |
| 104 |
self.extend([self.default] * (index + 1 - len(self))) |
| 105 |
|
| 106 |
def __setitem__(self, index, value): |
| 107 |
if isinstance(index, slice): |
| 108 |
super().__setitem__(index, value) |
| 109 |
else: |
| 110 |
if index < 0: |
| 111 |
super().__setitem__(index, value) |
| 112 |
else: |
| 113 |
self._extend_to(index) |
| 114 |
super().__setitem__(index, value) |
| 115 |
|
| 116 |
def insert(self, index, value): |
| 117 |
self._extend_to(index) |
| 118 |
super().insert(index, value) |