-
-
Save moqdm/5155d40c099fbc1e0f453bcfce60225e to your computer and use it in GitHub Desktop.
| # 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() |
I think you misunderstood the logic behind it. The simulate_tournament function is called recursively until only one team is left in the list "teams". read line 67 again "if rounds >= 2" means if the amount of teams left in list team is greater or equal to 2.
what is the answers for the answers.txt i am struggling
what is the answers for the answers.txt i am struggling
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
I know this was 13 months ago, but there was a โbugโ in this program.
In your function simulate tournament, the else statement just automatically prints the first team in the last list of two teams as the winner.
Instead in the else statement, you should input the last two teams into a simulate round function and then print the winner of that.