Skip to content

Instantly share code, notes, and snippets.

@mtigas
Created March 2, 2012 20:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtigas/1961114 to your computer and use it in GitHub Desktop.
Save mtigas/1961114 to your computer and use it in GitHub Desktop.
lambda assisted elections magic
# 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