-
Notifications
You must be signed in to change notification settings - Fork 5
/
utility.py
49 lines (39 loc) · 1.11 KB
/
utility.py
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
import itertools as it
from bisect import bisect_left
def reverse(s):
return s[-1::-1]
def clean(s):
return s.strip()
def flatten(list_of_lists):
"Flatten one level of nesting"
return it.chain.from_iterable(list_of_lists)
def take(n, iterable):
"Return first n items of the iterable as a list"
return list(it.islice(iterable, n))
def nth(n, iterable, default=None):
"Returns the nth item or a default value"
return next(it.islice(iterable, n, None), default)
def accumulate(iterable, func=lambda a,b: a+b):
'Return running totals'
# accumulate([1,2,3,4,5]) --> 1 3 6 10 15
# accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
iterable = iter(iterable)
total = next(iterable)
yield total
for element in iterable:
total = func(total, element)
yield total
def get_index(a, x):
'Locate the leftmost value exactly equal to x'
i = bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
return -1
class array_adaptor:
def __init__(self, lam, n):
self.lam = lam
self.n = n
def __getitem__(self, idx):
return self.lam(idx)
def __len__(self):
return self.n