Skip to content

Instantly share code, notes, and snippets.

@bdon
Created December 10, 2016 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bdon/1288d93c45965942ed4373574a5abcc6 to your computer and use it in GitHub Desktop.
Save bdon/1288d93c45965942ed4373574a5abcc6 to your computer and use it in GitHub Desktop.
stv_to_block.py
import sys
filename = sys.argv[1]
# Takes a STV blt file. Stripe out invalid ballots and leading numbers () first.
f = open(filename,'r')
totals = {}
end_votes_reached = False
candidates = []
seats = None
for index, line in enumerate(f):
line = line.strip()
if index == 0:
s = line.split(" ")
seats = int(s[1])
print "{0} candidates running for {1} seats.".format(s[0],seats)
# seed the totals with 0 for each candidate.
# candidates are numbered starting at 1
for i in range(1,int(s[0])+1):
totals[str(i)] = 0
else:
if line[0] == '1':
# it's a ballot
line = line.split(" ")
# take the top 4 choices.
# ignore the leading 0 and trailing 1.
# Count the top "seats" choices as equal votes.
for vote in line[1:-1][0:seats]:
totals[vote] = totals[vote] + 1
elif line[0] == '0':
end_votes_reached = True
elif end_votes_reached:
candidates.append(line)
for key in totals:
print "{0}: {1}".format(candidates[int(key)-1],totals[key])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment