-
Notifications
You must be signed in to change notification settings - Fork 14
/
constant_bin_number.py
72 lines (62 loc) · 2.25 KB
/
constant_bin_number.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from binpacking.to_constant_bin_number import to_constant_bin_number
def test_only_zero_weights():
values = [0, 0, 0]
bins = to_constant_bin_number(values, 4)
assert bins == [[0, 0, 0], [], [], []]
def test_weight_pos():
values = [
[1, 'x'],
[2, 'y'],
[1, 'z'],
]
bins = to_constant_bin_number(values, 2, weight_pos=0)
for bin_ in bins:
for item in bin_:
assert isinstance(item[0], int)
assert isinstance(item[1], str)
def test_key_func():
values = [
{'x': 'a', 'y': 1},
{'x': 'b', 'y': 5},
{'x': 'b', 'y': 3},
]
bins = to_constant_bin_number(values, 2, key=lambda x: x['y'])
for bin_ in bins:
for item in bin_:
assert 'x' in item
assert 'y' in item
def test_bounds_and_tuples():
c = [ ('a', 10, 'foo'), ('b', 10, 'log'), ('c', 11), ('d', 1, 'bar'), ('e', 2, 'bommel'), ('f',7,'floggo') ]
N_bin = 4
bins = to_constant_bin_number(c,N_bin,weight_pos=1,upper_bound=11)
bins = [ sorted(_bin, key=lambda x:x[0]) for _bin in bins ]
assert bins == [
[('a', 10, 'foo')],
[('b', 10, 'log')],
[('f', 7, 'floggo')],
[
('d', 1, 'bar'),
('e', 2, 'bommel'),
]
]
bins = to_constant_bin_number(c,N_bin,weight_pos=1,lower_bound=1)
bins = [ sorted(_bin, key=lambda x:x[0]) for _bin in bins ]
assert bins == [
[('c', 11,)],
[('a', 10, 'foo')],
[('b', 10, 'log')],
[
('e', 2, 'bommel'),
('f', 7, 'floggo'),
],
]
bins = to_constant_bin_number(c,N_bin,weight_pos=1,lower_bound=1,upper_bound=11)
bins = [ sorted(_bin, key=lambda x:x[0]) for _bin in bins ]
assert bins == [
[('a', 10, 'foo')],
[('b', 10, 'log')],
[('f', 7, 'floggo')],
[('e', 2, 'bommel')],
]
if __name__=="__main__":
test_bounds_and_tuples()