Skip to content

Instantly share code, notes, and snippets.

@dgfitch
Last active March 17, 2022 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgfitch/9809a1c43a17c24631697cc825bf1fce to your computer and use it in GitHub Desktop.
Save dgfitch/9809a1c43a17c24631697cc825bf1fce to your computer and use it in GitHub Desktop.
Simple shredder for Opavote BLT format ranked choice ballots
#!/usr/bin/env python3
import sys
with open(sys.argv[1], 'r') as ballot_file:
lines = [line.rstrip() for line in ballot_file]
# Trim first header and final line
lines.pop(0)
lines.pop()
ballots = []
options = []
is_ballot = True
for x in lines:
if x == "0":
is_ballot = False
continue
if is_ballot:
ranking = x.split(" ")
first = ranking.pop(0)
if first != "1":
print("Expected first thing in ballot to be 1")
sys.exit()
last = ranking.pop()
if last != "0":
print("Expected last thing in ballot to be 0")
sys.exit()
ranking = [int(x) for x in ranking]
ballots.append(ranking)
else:
options.append(x)
def count_in_ballots(thing):
count = 0
for b in ballots:
if thing in b:
count += 1
return count
def count_first(thing):
count = 0
for b in ballots:
if len(b) == 0:
continue
if thing == b[0]:
count += 1
return count
for index, o in enumerate(options):
option_number = index + 1
percentage_on_ballots = count_in_ballots(option_number) / len(ballots) * 100.0
percentage_first_on_ballots = count_first(option_number) / len(ballots) * 100.0
print(f"Option {option_number}: {o} had {percentage_on_ballots:.2f}% participation and was {percentage_first_on_ballots:.2f}% of voter's first choice")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment