Created
March 2, 2012 20:36
-
-
Save mtigas/1961114 to your computer and use it in GitHub Desktop.
lambda assisted elections magic
This file contains hidden or 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
# GIVEN: | |
# `counties` is a dict with each key being the slug of a county, | |
# containing a sub-dict with keys including vote counts and stuff. | |
# each candidate's vote count is stored in a key that is the candidate's | |
# name as a slug | |
# find vote leader for each county and store it in a 'leader' key for that county | |
for county_slug in counties.iterkeys(): | |
vals = [(candidate, counties[county_slug][candidate]) for candidate in CANDIDATES] | |
vals.sort(key=lambda c: c[1], reverse=True) | |
if vals[0][1] == 0: | |
# County with no results yet | |
counties[county_slug]['leader'] = "tie" | |
elif vals[0][1] == vals[1][1]: | |
# County with a tie at the top (at least a two-way tie) | |
counties[county_slug]['leader'] = "tie" | |
else: | |
counties[county_slug]['leader'] = vals[0][0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment