-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,4 @@ pset6/dna/sequences* | |
pset7/movies/movies.db | ||
pset7/houses/characters.csv | ||
pset7/houses/students.db | ||
*.csv |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Simulate a sports tournament | ||
|
||
import csv | ||
import sys | ||
import random | ||
|
||
# Number of simluations to run | ||
N = 1000 | ||
|
||
|
||
def main(): | ||
|
||
# Ensure correct usage | ||
if len(sys.argv) != 2: | ||
sys.exit("Usage: python tournament.py FILENAME") | ||
|
||
teams = [] | ||
# Read teams into memory from file | ||
filename = sys.argv[1] | ||
with open(filename) as csvfile: | ||
reader = csv.DictReader(csvfile) | ||
for row in reader: | ||
team = row | ||
team["rating"] = int(team["rating"]) | ||
teams.append(team) | ||
|
||
counts = {} | ||
# Simulate N tournaments and keep track of win counts | ||
for simulation in range(N): | ||
winner = simulate_tournament(teams) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Mayconpm
Author
Owner
|
||
if winner in counts: | ||
counts[winner] = counts[winner] + 1 | ||
else: | ||
counts[winner] = 1 | ||
|
||
# Print each team's chances of winning, according to simulation | ||
for team in sorted(counts, key=lambda team: counts[team], reverse=True): | ||
print(f"{team}: {counts[team] * 100 / N:.1f}% chance of winning") | ||
|
||
|
||
def simulate_game(team1, team2): | ||
"""Simulate a game. Return True if team1 wins, False otherwise.""" | ||
rating1 = team1["rating"] | ||
rating2 = team2["rating"] | ||
probability = 1 / (1 + 10 ** ((rating2 - rating1) / 600)) | ||
return random.random() < probability | ||
|
||
|
||
def simulate_round(teams): | ||
"""Simulate a round. Return a list of winning teams.""" | ||
winners = [] | ||
|
||
# Simulate games for all pairs of teams | ||
for i in range(0, len(teams), 2): | ||
if simulate_game(teams[i], teams[i + 1]): | ||
winners.append(teams[i]) | ||
else: | ||
winners.append(teams[i + 1]) | ||
|
||
return winners | ||
|
||
|
||
def simulate_tournament(teams): | ||
"""Simulate a tournament. Return name of winning team.""" | ||
rounds = len(teams) | ||
if rounds >= 2: | ||
teams = simulate_round(teams) | ||
return simulate_tournament(teams) | ||
else: | ||
winner = teams[0]["team"] | ||
return winner | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
simulate_tournament(teams) keeps saying undefined in my code