Last active
October 3, 2017 21:44
-
-
Save zachwhaley/7878df309c66798078bde6a5d998cc84 to your computer and use it in GitHub Desktop.
Ranked Choice Voting script
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import json | |
def tally(voters): | |
results = {} | |
for votes in voters.itervalues(): | |
if votes: | |
choice = votes[0] | |
count = results[choice] if choice in results else 0 | |
results[choice] = count + 1 | |
return results | |
with open('votes.json') as f: | |
voters = json.load(f) | |
round = 1 | |
winner = False | |
while not winner: | |
print 'Round {}'.format(round) | |
results = tally(voters) | |
if not results: | |
print '\nAll choices eliminated' | |
print 'No winner...' | |
break | |
high = -1 | |
low = sys.maxsize | |
lead = '' | |
for choice, count in results.iteritems(): | |
if count > high: | |
lead = choice | |
high = count | |
low = min(low, count) | |
perc = float(count) / float(len(voters)) | |
print ' {} - {} votes {:.1%}'.format(choice, count, perc) | |
perc = float(results[lead]) / float(len(voters)) | |
if perc > 0.5: | |
print '\n{} is the Winner!'.format(lead) | |
winner = True | |
elif len(results) == 2: | |
print '\nTie!' | |
winner = True | |
else: | |
print '\nNo winner' | |
elim = [choice for choice, count in results.iteritems() if count <= low] | |
for votes in voters.itervalues(): | |
if votes[0] in elim: | |
votes.pop(0) | |
for voter, votes in voters.items(): | |
if not votes: | |
del voters[voter] | |
round += 1 | |
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
{ | |
"trevor": | |
[ | |
"Hitchhiker's Guide to the Galaxy", | |
"Jurassic Park", | |
"The Martian", | |
"A Game of Thrones", | |
"The Gunslinger", | |
"Ender's Game" | |
], | |
"duane": | |
[ | |
"Hitchhiker's Guide to the Galaxy", | |
"A Game of Thrones", | |
"The Martian", | |
"Ancillary Justice", | |
"The Gunslinger", | |
"Ender's Game", | |
"Jurassic Park" | |
], | |
"kyle": | |
[ | |
"A Game of Thrones", | |
"Hitchhiker's Guide to the Galaxy", | |
"The Martian", | |
"Ancillary Justice", | |
"The Gunslinger", | |
"Ender's Game", | |
"Jurassic Park" | |
], | |
"thomas": | |
[ | |
"Ancillary Justice", | |
"Ender's Game", | |
"The Martian", | |
"Hitchhiker's Guide to the Galaxy", | |
"The Gunslinger", | |
"Jurassic Park", | |
"A Game of Thrones" | |
], | |
"zach": | |
[ | |
"A Game of Thrones", | |
"Hitchhiker's Guide to the Galaxy", | |
"Ender's Game", | |
"Ancillary Justice" | |
], | |
"ani": | |
[ | |
"Ancillary Justice", | |
"A Game of Thrones", | |
"Hitchhiker's Guide to the Galaxy", | |
"The Martian", | |
"The Gunslinger", | |
"Ender's Game", | |
"Jurassic Park" | |
], | |
"sayalee": | |
[ | |
"A Game of Thrones", | |
"Ancillary Justice", | |
"Hitchhiker's Guide to the Galaxy", | |
"The Martian", | |
"The Gunslinger", | |
"Ender's Game", | |
"Jurassic Park" | |
], | |
"tolunay": | |
[ | |
"The Martian", | |
"A Game of Thrones", | |
"Ancillary Justice", | |
"Hitchhiker's Guide to the Galaxy", | |
"Ender's Game", | |
"The Gunslinger", | |
"Jurassic Park" | |
], | |
"tave": | |
[ | |
"Ancillary Justice", | |
"Hitchhiker's Guide to the Galaxy", | |
"The Martian", | |
"The Gunslinger", | |
"Ender's Game", | |
"A Game of Thrones", | |
"Jurassic Park" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment