1

Check50 says, and I qoute, "timed out while waiting for program to exit" in some of the criterias, I don't understand what infinite loop is causing this. Can you help?

# Simulate a sports tournament

import csv import sys import random

# Number of simluations to run N = 1000000


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
    """ with closes the file after we are finished"""
    filename = sys.argv[1]
    with open(filename) as file:
        reader = csv.DictReader(file)
        for team in reader:
            """ access the value for a particular key inside the dictionary"""
            team["rating"] = int(team["rating"])
            teams.append(team)

    counts = {}
    # TODO: Simulate N tournaments and keep track of win counts
    for i 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
    """ used to simulate while there isn't one team left"""
    while len(teams) > 1:
        teams = simulate_round(teams)
    return teams[0]["team"]


if __name__ == "__main__":
    main()
3
  • We need some debugging information. What was the scenario used that led to this time out? Can you try using some example cases and values of N first to show what the times you are getting is?
    – UpAndAdam
    Aug 31, 2023 at 14:53
  • I've entered your code into it myself and even on the million case its only taking just over 6 seconds so I am very surprised you are getting a time out and indeed get a timeout with check50 if i use 1 million, but it seems to be fine for 100,000.
    – UpAndAdam
    Aug 31, 2023 at 15:42
  • IMHO they really should have made N a command line argument
    – UpAndAdam
    Aug 31, 2023 at 15:44

1 Answer 1

1

The problem is that for whatever reason the evaluator is set up such that you can't leave N set to 1000000 for it to pass, it takes too much time. There is no infinite loop. There is nothing wrong with your code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .