Created
August 30, 2021 15:42
-
-
Save moqdm/5155d40c099fbc1e0f453bcfce60225e to your computer and use it in GitHub Desktop.
it's my Lab 6: World Cup solution... *Please just take a look if you couldn't solve it ... Thank you π #cs50
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
| # 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 = [] | |
| # TODO: 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 = {} | |
| # TODO: Simulate N tournaments and keep track of win counts | |
| for simulation in range(N): | |
| winner = simulate_tournament(teams) | |
| if winner in counts: | |
| 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.""" | |
| # TODO | |
| 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was in the same case, but trying some different analysis and answers, and I came across these answers which were rated "OK" on cs50 check:
Questions:
Which predictions, if any, proved incorrect as you increased the number of simulations?: Simulations with N equal to 100000 and 1000000, as they have values of countries with a 0% chance of winning the world cup.
Suppose you're charged a fee for each second of compute time your program uses.
After how many simulations would you call the predictions "good enough"?: Depois de 1000 simulaΓ§Γ΅es.
The funny thing is that in the second answer I wrote in Portuguese and it was accepted, but when I translated it into English it wasn't.
I hope it helped you