-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
87 lines (61 loc) · 2.47 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
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
from functools import reduce
from random import randint, shuffle
def generate_int_list(total, count, min_value_allowed, max_value_allowed):
if total < count:
raise ValueError("Total must be greater than or equal to count")
if max_value_allowed < min_value_allowed:
raise ValueError("max_value_allowed must be greater than min_value_allowed")
expected_average = total / count
integer_list = []
numbers = list(range(min_value_allowed, max_value_allowed))
shuffle(numbers)
integer_list = numbers[:count]
list_total = sum(integer_list)
while list_total != total:
if list_total < total:
balance = total - list_total
for i, v in enumerate(integer_list):
if v + balance < max_value_allowed:
integer_list[i] = v + balance
break
diff = max_value_allowed - v
if v + balance > max_value_allowed:
integer_list[i] = max_value_allowed
balance = balance - diff
else:
integer_list[i] = v + diff
break
if list_total > total:
balance = list_total - total
for i, v in enumerate(integer_list):
if v - balance > min_value_allowed:
integer_list[i] = v - balance
break
if v > min_value_allowed and v - balance < max_value_allowed:
diff = v - min_value_allowed
integer_list[i] = min_value_allowed
balance = balance - diff
list_total = sum(integer_list)
if list_total != total:
shuffle(numbers)
integer_list = numbers[:count]
list_total = sum(integer_list)
return integer_list
# while True:
# integer_list = [randint(min_value_allowed, max_value_allowed) for i in range(count)]
# avg = reduce(lambda x, y: x + y, integer_list) / len(integer_list)
#
# if avg == expected_average:
# return integer_list
def generate_rand_list(lowest, highest, length):
output_list = []
for i in range(length):
output_list.append(randint(lowest, highest))
print(output_list)
if __name__ == '__main__':
int_list = generate_int_list(468, 18, 1, 32)
print(int_list),
print(len(int_list))
int_list = generate_int_list(714, 17, 1, 50)
print(int_list),
print(len(int_list))