Skip to content

Instantly share code, notes, and snippets.

@william-silversmith
Last active April 27, 2021 06:29
Show Gist options
  • Save william-silversmith/b732193e775d801e6d3f43b9df3cc371 to your computer and use it in GitHub Desktop.
Save william-silversmith/b732193e775d801e6d3f43b9df3cc371 to your computer and use it in GitHub Desktop.
Movie Club Borda Count Voting
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)
@william-silversmith
Copy link
Author

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

  1. In Google Sheets, go to the sheet with the current vote markings. Go to File > Download as... > Tab-separated values (tsv, current sheet)

screenshot from 2018-04-17 18 04 52

  1. 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.

  2. Save this sheet as another csv or tsv and run the script against it (see below).

Running the Script

Both tab separated values (tsv) and comma separated values (csv) files are supported.

python borda.py VOTING_FILE.csv

This will for example output:

CONGRATULATIONS Brick WITH 64 POINTS
----
1: Brick                                                                      64              19.75%
2: Death of Stalin                                                            50              15.43%
3: Millennium Actress                                                         40              12.35%
4: Grand Budapest Hotel                                                       38              11.73%
5: The Seventh Seal                                                           32               9.88%
6: Her                                                                        32               9.88%
7: F for Fake                                                                 32               9.88%
8: Ghost in the Shell (1995)                                                  12               3.70%
9: Matrix                                                                     10               3.09%
10: The 13th Warrior                                                           7               2.16%
11: Dredd                                                                      7               2.16%
12: 11:14                                                                      0               0.00%
13: Lucky # Slevin                                                             0               0.00%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment