Skip to content

Instantly share code, notes, and snippets.

@shaneshifflett
Created December 18, 2011 22:32
Show Gist options
  • Save shaneshifflett/1494681 to your computer and use it in GitHub Desktop.
Save shaneshifflett/1494681 to your computer and use it in GitHub Desktop.
rcv main loop
#keep track of the round number
round = 1
while next_round:
if round == 1:
#the world is our oyster, keep track of all the candidates still in play and their 1st,2nd,3rd place votes
avail_candidate_list = all_candidates
#all the ballots in the race
ballots = all_ballots
dropped_candidates = []
else:
#list of the candidate_ranking objects containing candidates we shouldn't use
dropped_candidates = eleminate_candidates(round -1)
#get a list of candidates still in the race, get_candidates_in_round(integer) returns a list of candidates not eliminated in a given round
avail_candidates_list = filter((lambda x: x not in dropped_candidates),
get_candidates_in_round(round -1))
#reclaim ballots from eliminated candidates
for c in dropped_candidates:
new_ballots = c.get_ballots()
for b in new_ballots:
ballots.append(b)
#redistribution process is heavily dependent on the way I wrote the data structures, using a generic function call to represent this process as to not obfuscate the primary logic
redistribute_ballots(avail_candidates_list, ballots)
#get the ballots still in play (non-exhausted, overvoted) for a given round
total_votes = get_continuing_ballots(round)
winning_threshold = (total_votes / 2) + 1
#get a list of candidates sorted by their total number of votes, most votes = first position in list
results = sort_contest(avail_candidates_list)
if len(results) != 0 and results[0].get_total_votes() >= winning_threshold or len(results) == 1:
#stop looping
next_round = False
//return the winning candidate
return results[0]
#next round!
round += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment