[Python] Uni project fprfixture generator in Python

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    teams = [
    ("Team 1", "Town 1", "Stadium St1"),
    ("Team 2", "Town 1", "Stadium St2"),
    ("Team 3", "Town 2", "Stadium St3"),
    ("Team 4", "Town 2", "Stadium St4"),
    ("Team 5", "Town 3", "Stadium St5"),
    ("Team 6", "Town 3", "Stadium St6"),
    ("Team 7", "Town 4", "Stadium St7"),
    ("Team 8", "Town 4", "Stadium St8"),
    ("Team 9", "Town 5", "Stadium St9"),
    ("Team 10", "Town 5", "Stadium St10"),
    ]


    In ABC premier league, there are 10 teams. You have been hired to build a program that will generate fixtures for these teams based on the following given rules.

    1. A team must play every other team twice, i.e. in their home stadium and away stadium. At some point, teams from the same local town will have to play each other, these teams should only meet after they have played teams from other local towns.
    2. Only 4 teams can play in a given weekend, for example Team1 vs Team3 and Team10 vs Team8.
    3. While developing the fixtures, indicate the local town, stadium, leg (i.e. 1 or 2) and the weekend (indicate as weekend #1, weekend #2 etc.) the teams are meant to meet.

    I am having difficult time. This is what I wrote in Python so far

    import itertools

    def generate_fixtures(teams):
    fixtures = []
    weeks = 1

    while weeks <= len(teams) - 1:
    week_fixtures = []

    # Generate fixtures for teams from different towns
    for pair in itertools.combinations(teams, 2):
    if pair[0][1] != pair[1][1] and pair[0][0] not in week_fixtures and pair[1][0] not in week_fixtures:
    fixture = f"Week {weeks}: {pair[0][0]} vs {pair[1][0]} (Leg 1, {pair[0][2]})"
    week_fixtures.extend([pair[0][0], pair[1][0]])
    fixtures.append(fixture)
    if len(week_fixtures) == 2:
    break

    # Generate fixtures for teams from the same town
    for town in set(team[1] for team in teams):
    teams_in_town = [team for team in teams if team[1] == town]
    for pair in itertools.combinations(teams_in_town, 2):
    if pair[0][0] not in week_fixtures and pair[1][0] not in week_fixtures:
    fixture = f"Week {weeks}: {pair[0][0]} vs {pair[1][0]} (Leg 2, {pair[0][2]})"
    week_fixtures.extend([pair[0][0], pair[1][0]])
    fixtures.append(fixture)
    if len(week_fixtures) == 2:
    break

    weeks += 1

    return fixtures

    # Define teams
    teams = [
    ("Team 1", "Town 1", "Stadium St1"),
    ("Team 2", "Town 1", "Stadium St2"),
    ("Team 3", "Town 2", "Stadium St3"),
    ("Team 4", "Town 2", "Stadium St4"),
    ("Team 5", "Town 3", "Stadium St5"),
    ("Team 6", "Town 3", "Stadium St6"),
    ("Team 7", "Town 4", "Stadium St7"),
    ("Team 8", "Town 4", "Stadium St8"),
    ("Team 9", "Town 5", "Stadium St9"),
    ("Team 10", "Town 5", "Stadium St10"),
    ]

    fixtures = generate_fixtures(teams)

    # Print fixtures
    for fixture in fixtures:
    print(fixture)

    Continue reading...

Compartilhe esta Página