-
Notifications
You must be signed in to change notification settings - Fork 5
/
Create_random_ballots.py
110 lines (98 loc) · 3.68 KB
/
Create_random_ballots.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import random
import itertools
import string, collections
def create_rand_ballots(voters=None, candidates=None, groups=None, max_score=None, compressed=True):
MAX_SCORE = 10
if max_score is None:
max_score = 3 # entered as 'plus one'
else:
if max_score > MAX_SCORE:
entered_max_score = max_score
max_score = MAX_SCORE
print(f" (!) Warning: max score reduced from {entered_max_score} to {MAX_SCORE} ")
elif max_score <= MAX_SCORE:
max_score = max_score
MAX_VOTERS = 99_999_999
if voters is None:
voters = random.randint(9, 22)
else:
if voters > MAX_VOTERS:
entered_max_voters = voters
voters = MAX_VOTERS
print(f" (!) Warning: voters reduced from {entered_max_voters} to {MAX_VOTERS} ")
elif voters <= MAX_VOTERS:
voters = voters
MAX_CANDIDATES = 50
if candidates is None:
candidates = random.randint(5, 5)
else:
if candidates > MAX_CANDIDATES:
entered_max_candidates = candidates
candidates = MAX_CANDIDATES
print(f" (!) Warning: candidates reduced from {entered_max_candidates} to {MAX_CANDIDATES} ")
elif candidates <= MAX_CANDIDATES:
candidates = candidates
if groups is None:
if candidates == 2:
groups = 1
elif candidates == 3:
groups = random.randint(1, 3)
elif candidates <= 6:
groups = random.randint(1, 3)
elif candidates > 6:
groups = random.randint(2, 4)
else:
entered_groups = groups
if groups > candidates:
print(f" (!) Warning: groups reduced from {entered_groups} to {candidates}")
groups = candidates
else:
groups = entered_groups
candidates_names_potential = list(string.ascii_uppercase + string.ascii_lowercase)
candidates_names = candidates_names_potential[0:candidates]
random.shuffle(candidates_names)
product = itertools.product(list(range(max_score)), repeat=candidates)
temp_prod = list(product)
ballots = random.choices(list(temp_prod), k=voters)
ballots_as_string = []
for e in list(ballots):
temp = str(e)
temp = temp.replace("(", "")
temp = temp.replace(")", "")
temp = temp.replace(" ", "")
ballots_as_string.append(temp)
c = collections.Counter(ballots_as_string)
final_as_string = "\n".join(ballots_as_string)
final_as_counter = ""
for e, i in c.items():
final_as_counter += f"{i}: {e} \n"
if compressed is True:
result = str(final_as_counter)
else:
result = final_as_string
# assign candidates to groups / affiliation
# candidate who remain unaffiliated are in special group 'zero'
temp_dict_with_groups = {}
for e in candidates_names:
temp = random.randint(0, groups)
temp = "Grp " + str(temp)
temp_dict_with_groups[temp] = temp_dict_with_groups.get(temp, '') + str(e)
temp_dict_with_groups = sorted(temp_dict_with_groups.items())
groups_as_table = ""
len_of_groups = len(temp_dict_with_groups)
for e in temp_dict_with_groups:
groups_as_table += "".join(e[0]) + ": " + "".join(e[1]) + "\n"
return (
"Number of Voters = " + str(voters),
"Max Score = " + str(max_score - 1),
"Affiliation Grps = " + str(groups),
"Affiliation and Candidates assignment: ",
groups_as_table,
"Number of Candidates = " + str(candidates),
",".join(sorted(candidates_names)),
result,
)
for e in range(3):
a = create_rand_ballots()
for e in a:
print(e)