summaryrefslogtreecommitdiff
path: root/list_utils.py
blob: 8f92be30c45fba8531654895f7a03da29b18dae7 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3

# © Copyright 2021-2022, Scott Gasch

"""Some useful(?) utilities for dealing with Lists."""

import random
from collections import Counter
from itertools import chain, combinations, islice
from typing import Any, Iterator, List, MutableSequence, Sequence, Tuple


def shard(lst: List[Any], size: int) -> Iterator[Any]:
    """
    Yield successive size-sized shards from lst.

    >>> for sublist in shard([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 3):
    ...     [_ for _ in sublist]
    [1, 2, 3]
    [4, 5, 6]
    [7, 8, 9]
    [10, 11, 12]

    """
    for x in range(0, len(lst), size):
        yield islice(lst, x, x + size)


def flatten(lst: List[Any]) -> List[Any]:
    """
    Flatten out a list:

    >>> flatten([ 1, [2, 3, 4, [5], 6], 7, [8, [9]]])
    [1, 2, 3, 4, 5, 6, 7, 8, 9]

    """
    if len(lst) == 0:
        return lst
    if isinstance(lst[0], list):
        return flatten(lst[0]) + flatten(lst[1:])
    return lst[:1] + flatten(lst[1:])


def prepend(item: Any, lst: List[Any]) -> List[Any]:
    """
    Prepend an item to a list.

    >>> prepend('foo', ['bar', 'baz'])
    ['foo', 'bar', 'baz']

    """
    lst.insert(0, item)
    return lst


def remove_list_if_one_element(lst: List[Any]) -> Any:
    """
    Remove the list and return the 0th element iff its length is one.

    >>> remove_list_if_one_element([1234])
    1234

    >>> remove_list_if_one_element([1, 2, 3, 4])
    [1, 2, 3, 4]

    """
    if len(lst) == 1:
        return lst[0]
    else:
        return lst


def population_counts(lst: Sequence[Any]) -> Counter:
    """
    Return a population count mapping for the list (i.e. the keys are
    list items and the values are the number of occurrances of that
    list item in the original list.

    >>> population_counts([1, 1, 1, 2, 2, 3, 3, 3, 4])
    Counter({1: 3, 3: 3, 2: 2, 4: 1})

    """
    return Counter(lst)


def most_common(lst: List[Any], *, count=1) -> Any:

    """
    Return the most common item in the list.  In the case of ties,
    which most common item is returned will be random.

    >>> most_common([1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4])
    3

    >>> most_common([1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4], count=2)
    [3, 1]

    """
    p = population_counts(lst)
    return remove_list_if_one_element([_[0] for _ in p.most_common()[0:count]])


def least_common(lst: List[Any], *, count=1) -> Any:
    """
    Return the least common item in the list.  In the case of
    ties, which least common item is returned will be random.

    >>> least_common([1, 1, 1, 2, 2, 3, 3, 3, 4])
    4

    >>> least_common([1, 1, 1, 2, 2, 3, 3, 3, 4], count=2)
    [4, 2]

    """
    p = population_counts(lst)
    mc = p.most_common()[-count:]
    mc.reverse()
    return remove_list_if_one_element([_[0] for _ in mc])


def dedup_list(lst: List[Any]) -> List[Any]:
    """
    Remove duplicates from the list performantly.

    >>> dedup_list([1, 2, 1, 3, 3, 4, 2, 3, 4, 5, 1])
    [1, 2, 3, 4, 5]

    """
    return list(set(lst))


def uniq(lst: List[Any]) -> List[Any]:
    """
    Alias for dedup_list.
    """
    return dedup_list(lst)


def contains_duplicates(lst: List[Any]) -> bool:
    """
    Does the list contian duplicate elements or not?

    >>> lst = [1, 2, 1, 3, 3, 4, 4, 5, 6, 1, 3, 4]
    >>> contains_duplicates(lst)
    True

    >>> contains_duplicates(dedup_list(lst))
    False

    """
    seen = set()
    for _ in lst:
        if _ in seen:
            return True
        seen.add(_)
    return False


def all_unique(lst: List[Any]) -> bool:
    """
    Inverted alias for contains_duplicates.
    """
    return not contains_duplicates(lst)


def transpose(lst: List[Any]) -> List[Any]:
    """
    Transpose a list of lists.

    >>> lst = [[1, 2], [3, 4], [5, 6]]
    >>> transpose(lst)
    [[1, 3, 5], [2, 4, 6]]

    """
    transposed = zip(*lst)
    return [list(_) for _ in transposed]


def ngrams(lst: Sequence[Any], n):
    """
    Return the ngrams in the sequence.

    >>> seq = 'encyclopedia'
    >>> for _ in ngrams(seq, 3):
    ...     _
    'enc'
    'ncy'
    'cyc'
    'ycl'
    'clo'
    'lop'
    'ope'
    'ped'
    'edi'
    'dia'

    >>> seq = ['this', 'is', 'an', 'awesome', 'test']
    >>> for _ in ngrams(seq, 3):
    ...     _
    ['this', 'is', 'an']
    ['is', 'an', 'awesome']
    ['an', 'awesome', 'test']
    """
    for i in range(len(lst) - n + 1):
        yield lst[i : i + n]


def permute(seq: str):
    """
    Returns all permutations of a sequence; takes O(N!) time.

    >>> for x in permute('cat'):
    ...     print(x)
    cat
    cta
    act
    atc
    tca
    tac

    """
    yield from _permute(seq, "")


def _permute(seq: str, path: str):
    seq_len = len(seq)
    if seq_len == 0:
        yield path

    for i in range(seq_len):
        car = seq[i]
        left = seq[0:i]
        right = seq[i + 1 :]
        cdr = left + right
        yield from _permute(cdr, path + car)


def shuffle(seq: MutableSequence[Any]) -> MutableSequence[Any]:
    """Shuffles a sequence into a random order.

    >>> random.seed(22)
    >>> shuffle([1, 2, 3, 4, 5])
    [3, 4, 1, 5, 2]

    >>> shuffle('example')
    'empaelx'

    """
    if isinstance(seq, str):
        import string_utils

        return string_utils.shuffle(seq)
    else:
        random.shuffle(seq)
        return seq


def scramble(seq: MutableSequence[Any]) -> MutableSequence[Any]:
    return shuffle(seq)


def binary_search(lst: Sequence[Any], target: Any) -> Tuple[bool, int]:
    """Performs a binary search on lst (which must already be sorted).
    Returns a Tuple composed of a bool which indicates whether the
    target was found and an int which indicates the index closest to
    target whether it was found or not.

    >>> a = [1, 4, 5, 6, 7, 9, 10, 11]
    >>> binary_search(a, 4)
    (True, 1)

    >>> binary_search(a, 12)
    (False, 8)

    >>> binary_search(a, 3)
    (False, 1)

    >>> binary_search(a, 2)
    (False, 1)

    >>> a.append(9)
    >>> binary_search(a, 4, sanity_check=True)
    Traceback (most recent call last):
    ...
    AssertionError

    """
    if __debug__:
        last = None
        for x in lst:
            if last is not None:
                assert x >= last  # This asserts iff the list isn't sorted
            last = x  # in ascending order.
    return _binary_search(lst, target, 0, len(lst) - 1)


def _binary_search(lst: Sequence[Any], target: Any, low: int, high: int) -> Tuple[bool, int]:
    if high >= low:
        mid = (high + low) // 2
        if lst[mid] == target:
            return (True, mid)
        elif lst[mid] > target:
            return _binary_search(lst, target, low, mid - 1)
        else:
            return _binary_search(lst, target, mid + 1, high)
    else:
        return (False, low)


def powerset(lst: Sequence[Any]) -> Iterator[Sequence[Any]]:
    """Returns the powerset of the items in the input sequence.

    >>> for x in powerset([1, 2, 3]):
    ...     print(x)
    ()
    (1,)
    (2,)
    (3,)
    (1, 2)
    (1, 3)
    (2, 3)
    (1, 2, 3)
    """
    return chain.from_iterable(combinations(lst, r) for r in range(len(lst) + 1))


if __name__ == '__main__':
    import doctest

    doctest.testmod()