-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuffersplit.py
More file actions
79 lines (68 loc) · 1.76 KB
/
buffersplit.py
File metadata and controls
79 lines (68 loc) · 1.76 KB
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
"""
Buffer/array/list splitting module
-----------------------------------
| Copyright 2022 by Joel C. Alcarez |
| [[email protected]] |
|-----------------------------------|
| We make absolutely no warranty |
| of any kind, expressed or implied |
|-----------------------------------|
| Contact primary author |
| if you plan to use this |
| in a commercial product at |
-----------------------------------
"""
def altsplitbuf(buf: list[any]
) -> list[any]:
"""Split a list into two with
alternating items in
different lists
Args:
buf: list, array or tuple
Returns:
[[odd list], [even list]]
"""
m = len(buf)
if m % 2 == 0:
m -= m & 1
return [buf[:m - 1:2], buf[1: m: 2]]
def altsplitbuf3way(buf: list[any]
) -> list[any]:
"""Split a list into three with
alternating items in
different lists
Args:
buf: list, array or tuple
[red, green, blue, ...]
Returns:
[[red list],
[green list],
[blue list]]
"""
m = len(buf)
if m % 3 == 0:
m -= m & 1
return [buf[:m - 2:3], buf[1: m - 1: 3], buf[2: m: 3]]
def altsplitbufnway(buf: list[any],
n: int) -> list[any]:
"""Split a list into n with
alternating items in
different lists
Args:
buf: list, array or tuple
[red, green, blue, ...]
Returns:
[[red list],
[green list],
[blue list]]
"""
retval = []
m = len(buf)
j = n - 1
if m % n == 0:
m -= m & 1
for i in range(n):
retval += [buf[i: m - j: n]]
j -= 1
return retval