Last active
April 27, 2021 06:29
-
-
Save william-silversmith/b732193e775d801e6d3f43b9df3cc371 to your computer and use it in GitHub Desktop.
Movie Club Borda Count Voting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import os | |
import sys | |
from collections import defaultdict | |
rows = [] | |
filename = sys.argv[1] | |
_, ext = os.path.splitext(filename) | |
if ext == '.csv': | |
delimiter = ',' | |
elif ext == '.tsv': | |
delimiter = '\t' | |
else: | |
raise NotImplementedError("Unknown file extension.") | |
with open(filename, 'r') as csvfile: | |
votereader = csv.reader(csvfile, delimiter=delimiter, quotechar=None) | |
for row in votereader: | |
rows.append(row) | |
headers = rows.pop(0) | |
voter_names = headers[:1] # MOVIES NAME_1 NAME_2 etc | |
num_candidates = len(rows) | |
tabulations = defaultdict(int) | |
for movie in rows: | |
title = movie[0] | |
votes = movie[1:] | |
for vote in votes: | |
if vote == '': | |
vote = num_candidates | |
vote = int(vote) | |
assert 0 <= vote <= num_candidates, "{} / {}".format(vote, num_candidates) | |
tabulations[title] += num_candidates - vote | |
winning_value = max(tabulations.values()) | |
winners = [ title for title, total in tabulations.items() if total == winning_value ] | |
if len(winners) > 1: | |
print "TIE BETWEEN " + ", ".join(winners) | |
else: | |
print "CONGRATULATIONS " + winners[0] + " WITH {} POINTS".format(winning_value) | |
sortable = [ (title, pts) for title, pts in tabulations.items() ] | |
sortable = sorted(sortable, key=lambda item: item[1]) | |
sortable.reverse() | |
total_pts = sum(map(lambda x: x[1], sortable)) | |
print "----" | |
def fixed_width_sprint(ct, title, pts): | |
pts = str(pts) | |
spaces = ct - len(title) - len(pts) | |
return title + (' ' * spaces) + pts | |
for index, movie in enumerate(sortable): | |
title, points = movie | |
percent = float(points) / float(total_pts) * 100 | |
movie_rank = str(index+1) + ": " + title | |
text = fixed_width_sprint(80, movie_rank, points) | |
print fixed_width_sprint(100, text, '%2.2f%%' % percent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Movie Club Borda Count Voting
This script is to facilitate tabulating votes for movie club. The voting system is ranked Borda counts. Each movie gets a single numerical vote (1,2,3,..) or no vote and gets assigned points based on the ranking or zero points if no ranking is provided.
Preparing the Votes
Open the file in a local spreadsheet editor (e.g. Microsoft Excel, Libreoffice Sheets, etc., even another Google Sheets file) and delete any extraneous markings including directions such that the only data are the labels of movies and voters, and the votes themselves.
Save this sheet as another
csv
ortsv
and run the script against it (see below).Running the Script
Both tab separated values (
tsv
) and comma separated values (csv
) files are supported.This will for example output: