-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborda.py
35 lines (27 loc) · 1.03 KB
/
borda.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
"""
Implements the Borda Count
"""
import time
import datetime
from collections import defaultdict
def borda_count(profile):
"""
Implements the Borda count by calculating all Borda scores
"""
print('\nApplying Borda Count to the Profile...')
# Start timer
time_start = time.perf_counter()
num_candidates = len(profile[0])
borda_scores = defaultdict(int)
for vote in profile:
for rank, candidate in enumerate(vote):
borda_scores[candidate] += (num_candidates - rank - 1)
sorted_scores = sorted(borda_scores.items(), key=lambda item: item[1], reverse=True)
final_ranking = [pair[0] for pair in sorted_scores]
print("The winning ranking is as follows: ")
winning_ranking_stringified = [str(i) for i in final_ranking]
print(", ".join(winning_ranking_stringified))
# Calculate time required to finish
time_finish = time.perf_counter()
time_elapsed = datetime.timedelta(seconds = (time_finish - time_start))
print(f"Applying the Borda Count took {time_elapsed}")