Skip to content

Commit a77519f

Browse files
committed
-
1 parent f4474a6 commit a77519f

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

source_py3/python_toolbox/sequence_tools/misc.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,24 @@ def partitions(sequence, partition_size=None, *, n_partitions=None,
5757
allow_remainder=True, larger_on_remainder=False,
5858
fill_value=NO_FILL_VALUE):
5959
'''
60-
Partition `sequence` into equal partitions of size `partition_size`, or
60+
Break `sequence` into partitions.
61+
62+
Partitions `sequence` into partitions of size `partition_size`, or
6163
determine size automatically given the number of partitions as
6264
`n_partitions`.
6365
64-
If the sequence can't be divided into precisely equal partitions, the last
65-
partition will contain less members than all the other partitions.
66+
This function attempts to break the sequence into partitions with an equal
67+
number of items; if the sequence can't be divided into precisely equal
68+
partitions, the first partitions will have more elements then the
69+
partitions that come after them.
6670
6771
Example:
6872
6973
>>> partitions([0, 1, 2, 3, 4], 2)
7074
[[0, 1], [2, 3], [4]]
7175
72-
(You need to give *either* a `partition_size` *or* an `n_partitions`
73-
argument, not both.)
76+
You need to give *either* a `partition_size` *or* an `n_partitions`
77+
argument, not both.
7478
7579
Specify `allow_remainder=False` to enforce that the all the partition sizes
7680
be equal; if there's a remainder while `allow_remainder=False`, an
@@ -101,6 +105,8 @@ def partitions(sequence, partition_size=None, *, n_partitions=None,
101105
sequence = ensure_iterable_is_sequence(sequence)
102106

103107
sequence_length = len(sequence)
108+
if 0 in (n_partitions, partition_size):
109+
raise ValueError
104110

105111
### Validating input: #####################################################
106112
# #

source_py3/test_python_toolbox/test_sequence_tools/test_partitions.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,34 @@ def test_different_types():
117117
assert partitions(sequence, partition_size=2,
118118
larger_on_remainder=True) == [
119119
sequence[:2], sequence[2:]
120-
]
120+
]
121+
122+
def test_n_partitions():
123+
r = list(range(10))
124+
with cute_testing.RaiseAssertor(ValueError):
125+
partitions(r, n_partitions=0)
126+
assert partitions(r, n_partitions=1) == [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
127+
assert partitions(r, n_partitions=2) == [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
128+
assert partitions(r, n_partitions=3) == \
129+
[[0, 1, 2, 3], [4, 5, 6,], [7, 8, 9]]
130+
assert partitions(r, n_partitions=4) == \
131+
[[0, 1, 2], [3, 4, 5], [6, 7], [8, 9]]
132+
assert partitions(r, n_partitions=5) == \
133+
[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
134+
assert partitions(r, n_partitions=6) == \
135+
[[0, 1], [2, 3], [4, 5], [6, 7], [8], [9]]
136+
assert partitions(r, n_partitions=7) == \
137+
[[0, 1], [2, 3], [4, 5], [6], [7], [8], [9]]
138+
assert partitions(r, n_partitions=8) == \
139+
[[0, 1], [2, 3], [4], [5], [6], [7], [8], [9]]
140+
assert partitions(r, n_partitions=9) == \
141+
[[0, 1], [2], [3], [4], [5], [6], [7], [8], [9]]
142+
assert partitions(r, n_partitions=10) == \
143+
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
144+
assert partitions(r, n_partitions=11) == \
145+
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], []]
146+
assert partitions(r, n_partitions=12) == \
147+
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [], []]
148+
149+
# blocktodo: add same batch of tests but with larger_on_remainder
150+

0 commit comments

Comments
 (0)