-
Notifications
You must be signed in to change notification settings - Fork 2
/
vote-counter.py
124 lines (93 loc) · 3.26 KB
/
vote-counter.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import json
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument('file')
args = parser.parse_args()
votes = get_votes(args.file)
for division, voting in sorted(votes.items()):
produce_division_report(division, voting)
def get_votes(filename):
with open(filename, 'r') as votes_file:
lines = [vote_line.strip().lower()
for vote_line in votes_file.readlines()
if vote_line not in ['', '\n']]
voting_divisions = {
'premier': {},
'intermediate': {},
'open': {},
'general': {}
}
current_division = ''
for line in lines:
category, votes = tuple(line.split(':'))
category = category.strip()
votes = votes.strip()
if category == 'your team division':
current_division = votes
elif category == 'your team name':
pass
else:
if category in ['premier mvp', 'best caster']:
current_division, temp_division = 'general', current_division
if len(votes.split(',')) > 1:
votes = votes.split(',')
else:
votes = [votes]
votes = [vote.strip() for vote in votes]
if category not in voting_divisions[current_division]:
voting_divisions[current_division][category] = []
voting_divisions[current_division][category] += [votes]
if current_division == 'general':
current_division = temp_division
return voting_divisions
def produce_division_report(division, voting):
print(division)
print('=' * len(division))
for vote_category, ballots in sorted(voting.items()):
winners = process_votes(ballots)
print(vote_category + ':', end=' ')
print(*winners, sep=' and ')
def get_counts(ballots):
first_votes = dict()
for vote_set in ballots:
if len(vote_set) > 0:
for i, vote in enumerate(vote_set):
if vote not in first_votes:
first_votes[vote] = 0
if i == 0:
first_votes[vote] += 1
return first_votes
def get_winners(ballots):
counts = get_counts(ballots)
max_count = max(counts.values())
num_counts = sum(counts.values())
potential_winners = [canidate for (canidate, count) in counts.items()
if count == max_count]
if max_count >= num_counts/2 or len(potential_winners) == len(counts):
return potential_winners
else:
return []
def get_losers(ballots):
counts = get_counts(ballots)
min_count = min(counts.values())
potential_losers = [canidate for (canidate, count) in counts.items()
if count == min_count]
if len(potential_losers) == len(counts):
return []
else:
return potential_losers
def remove_canidate(ballots, canidate):
for ballot in ballots:
while canidate in ballot:
ballot.remove(canidate)
def process_votes(ballots):
while True:
winners = get_winners(ballots)
if winners:
break
losers = get_losers(ballots)
for loser in losers:
remove_canidate(ballots, loser)
return winners
main()