Skip to content

Commit

Permalink
Lab6
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayconpm committed Jan 6, 2021
1 parent 97f17b7 commit cb2f8f0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ pset6/dna/sequences*
pset7/movies/movies.db
pset7/houses/characters.csv
pset7/houses/students.db
*.csv
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## [Academic Honesty](https://cs50.harvard.edu/x/2021/honesty/)

- [Problem Set 0: Scratch :smiley_cat:](https://scratch.mit.edu/projects/469716350)
- [Problem Set 0: Scratch :smiley_cat:](https://scratch.mit.edu/projects/469716350)

- [Lab 1: Population Growth](/lab1)

Expand Down Expand Up @@ -46,6 +46,8 @@

- [speller](/pset5/speller)

[Lab 6: World Cup :soccer:](/lab6)

- [Problem Set 6: Python :snake:](/pset6)

- [hello](/pset6/hello)
Expand All @@ -59,4 +61,4 @@

- [Problem Set 7: SQL](/pset7)

- [movies :cinema:](pset7/movies)
- [movies :cinema:](pset7/movies)
75 changes: 75 additions & 0 deletions lab6/tournament.py
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.

Copy link
@Tomileye

Tomileye Feb 9, 2022

simulate_tournament(teams) keeps saying undefined in my code

This comment has been minimized.

Copy link
@Mayconpm

Mayconpm Feb 9, 2022

Author Owner

If you followed the code here, you shouldn't be getting this error, make sure you defined the simulate_round(teams) function

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()

0 comments on commit cb2f8f0

Please sign in to comment.