Last active
March 15, 2017 12:36
-
-
Save FZambia/1cf0502aac59a968ee506e42d082ede3 to your computer and use it in GitHub Desktop.
Play-off simulation
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 sys | |
| import math | |
| import uuid | |
| import random | |
| import pprint | |
| def _get_random_game_score(): | |
| max_score = 5 | |
| score1 = score2 = None | |
| while score1 == score2: | |
| score1, score2 = random.randint(0, max_score), random.randint(0, max_score) | |
| return score1, score2 | |
| def _run_stage_simulation(stage_teams): | |
| stats = { | |
| "games": {}, | |
| "teams": {} | |
| } | |
| winners = [] | |
| random.shuffle(stage_teams) | |
| num_games = len(stage_teams) // 2 | |
| for _ in range(num_games): | |
| game_id = uuid.uuid4().hex | |
| team1 = stage_teams.pop() | |
| team2 = stage_teams.pop() | |
| score1, score2 = _get_random_game_score() | |
| stats["teams"][team1] = game_id | |
| stats["teams"][team2] = game_id | |
| stats["games"][game_id] = { | |
| team1: score1, | |
| team2: score2 | |
| } | |
| winners.append(team1 if score1 > score2 else team2) | |
| return stats, winners | |
| def run_playoff_simulation(teams): | |
| """ | |
| Runs playoff simulation for a given list of teams. | |
| List of teams must be already validated. | |
| """ | |
| stats = [] | |
| stage_teams = teams[:] | |
| while len(stage_teams) >= 2: | |
| stage_stats, winners = _run_stage_simulation(stage_teams) | |
| stats.append(stage_stats) | |
| stage_teams = winners | |
| return stats | |
| def validate_team_list(teams): | |
| """ | |
| Validates team list to make sure it's suitable for playoff | |
| simulation. | |
| """ | |
| if len(set(teams)) != len(teams): | |
| # non-unique team found in list. | |
| return False | |
| # number of teams must be a power of two. | |
| return math.log2(len(teams)).is_integer() | |
| if __name__ == "__main__": | |
| teams = [ | |
| "Russia", "Germany", "Australia", "Chile", | |
| "Mexico", "New Zealand", "Portugal", "Cameroon" | |
| ] | |
| if not validate_team_list(teams): | |
| print("Malformed team list") | |
| sys.exit(1) | |
| stats = run_playoff_simulation(teams) | |
| pprint.pprint(stats) | |
| team = input("Enter team you are interested in: ") | |
| if team not in teams: | |
| print("Team not found") | |
| sys.exit(1) | |
| for stage_stats in stats: | |
| if team not in stage_stats["teams"]: | |
| break | |
| stage_game_num = len(stage_stats["games"]) | |
| stage_name = "1/{}".format(stage_game_num) if stage_game_num > 1 else "Final" | |
| game_id = stage_stats["teams"][team] | |
| game_stats = stage_stats["games"][game_id] | |
| team1, team2 = game_stats.keys() | |
| score1 = game_stats[team1] | |
| score2 = game_stats[team2] | |
| print(f"{stage_name}: {team1} {score1}:{score2} {team2}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment