Created
March 25, 2014 17:51
-
-
Save mcreenan/9767332 to your computer and use it in GitHub Desktop.
Blades Winter Playoff Scenarios
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
import itertools | |
import operator | |
team_to_check = 'Blades' | |
current_team_standings = {'Niagara Hitmen': 14.0, | |
'Icy Red South': 10.0, | |
'Highlanders': 9.5, | |
'Blades': 9.0, | |
'Crashers': 8.5, | |
'Puckheads': 6.5, | |
'Growlers': 5.0, | |
'Wolverines': 2.0, | |
'Cyclones': 0.5, } | |
games_left = [ | |
{'home': 'Crashers', 'away': 'Icy Red South'}, | |
{'home': 'Cyclones', 'away': 'Niagara Hitmen'}, | |
{'home': 'Growlers', 'away': 'Highlanders'}, | |
{'home': 'Crashers', 'away': 'Cyclones'}, | |
{'home': 'Blades', 'away': 'Wolverines'}, | |
] | |
def get_game_result_combinations(home, away): | |
return [{home: 5.0, away: 0.0}, | |
{home: 4.5, away: 0.5}, | |
{home: 4.0, away: 1.0}, | |
{home: 3.5, away: 1.5}, | |
{home: 3.0, away: 2.0}, | |
{home: 2.5, away: 2.5}, | |
{home: 2.0, away: 3.0}, | |
{home: 1.5, away: 3.5}, | |
{home: 1.0, away: 4.0}, | |
{home: 0.5, away: 4.5}, | |
{home: 0.0, away: 5.0}] | |
def format_game_results(results): | |
games = [] | |
for game in results: | |
home_team, away_team = set(game) | |
if game[home_team] == game[away_team]: | |
games.append('{0} ({1}pts) tie {2} ({3}pts)'.format( | |
home_team, game[home_team], away_team, game[away_team])) | |
elif game[home_team] > game[away_team]: | |
games.append('{0} ({1}pts) beat {2} ({3}pts)'.format( | |
home_team, game[home_team], away_team, game[away_team])) | |
elif game[home_team] < game[away_team]: | |
games.append('{0} ({1}pts) beat {2} ({3}pts)'.format( | |
away_team, game[away_team], home_team, game[home_team])) | |
return ' and '.join(games) | |
game_results = [get_game_result_combinations(games_left[i]['home'], games_left[i]['away']) for i in range(0, len(games_left))] | |
selection_product = list(itertools.product(range(0, 11), repeat=len(games_left))) | |
win_scenarios = 0 | |
for result_selection in selection_product: | |
final_team_standings = current_team_standings.copy() | |
results = [game_results[game_number][result_selection[game_number]] for game_number in range(0, len(games_left))] | |
for game in results: | |
for team_name, points_earned in game.iteritems(): | |
final_team_standings[team_name] += points_earned | |
sorted_standings = sorted(final_team_standings.iteritems(), key=operator.itemgetter(1), reverse=True) | |
first_place_team = sorted_standings[0][0] | |
second_place_team = sorted_standings[1][0] | |
if first_place_team == team_to_check or second_place_team == team_to_check: | |
win_scenarios += 1 | |
print "1. {team1} ({team1_pts}) / 2. {team2} ({team2_pts}) if {results}".format( | |
team1=first_place_team, team1_pts=final_team_standings[first_place_team], | |
team2=second_place_team, team2_pts=final_team_standings[second_place_team], | |
results=format_game_results(results)) | |
print "There are {win_scenarios} scenarios for the {team} to make the championship (out of {total} total scenarios)".format(win_scenarios=win_scenarios, team=team_to_check, total=len(selection_product)) | |
print "That is a {:.2%} chance".format(float(win_scenarios) / float(len(selection_product))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment