Football fixture algorithm

Viewed 67

I've been working on this algorithm for some time now (honestly a very long time), and I can't seem to see through what I'm missing. The algorithm is an attempt to make all the possible fixtures in a 20-teams League (EPL) if all teams are to play each other home and away once a week. That'll make 10 matches a week if all teams play, 38 games per team (38 weeks) that's 380 games total. The challenge is; that a team can't play twice in a match week (20 teams can only make 10 matches), and no same fixture is to be repeated. The problem: In other to avoid any repetition, I check if the fixture and both teams have not been previously selected, But then the result shows 10 games each for the first 6 weeks, 8 for the next 9, 10 for the next 3, to 4 games for the last 2 weeks. Please what am I missing here?

teams = ["ARS", "AVL", "BRE", "BOU", "BRI",
        "BUR", "CHE", "CRY", "FUL",
        "EVE", "LEE", "LEI", "LIV", "MCI", "MUN",
        "NEW", "TOT", "WHU", "WOL", "WAT"]

#All 380 possible fixtures len(fixtures) prints 380
fixtures =  [f"{home} - {away}" for home in teams for away in teams if home != away]

#38 match weeks if 10 matches are played everyweek. Each team plays only once per week
match_weeks = []

#Fixtures played // no same fixture is to be played twice
#i.e ARS - CHE can appear only once per seoson, but CHE - ARS only
s_fixtures = []

#Making fixtures for all 38 weeks
for i in range(38):
    match_week = [] #should be exactly 10 uniq games every week = 20/2
    s_teams = [] #teams already selected in the current week // 20 teams every week
    for fixture in fixtures:
        if len(match_week) == 10: #to save some iterations/time 
            break
        if fixture not in s_fixtures:
            home, away = fixture[:3], fixture[6:]
            if home not in s_teams and away not in s_teams:
                s_teams.extend([home, away]) # add teams to selected list
                print(len(s_teams))
                match_week.append(fixture)
                s_fixtures.append(fixture) #add fixture to selected list
    match_weeks.append(match_week)
1 Answers

Just showing my implementation and getting same as you. I'l continue to work on it and try to figure this out.

import random

teams = ["ARS", "AVL", "BRE", "BOU", "BRI",
        "BUR", "CHE", "CRY", "FUL",
        "EVE", "LEE", "LEI", "LIV", "MCI", "MUN",
        "NEW", "TOT", "WHU", "WOL", "WAT"]


fixtures =  [f"{home} - {away}" for home in teams for away in teams if home != away]
full_fixtures = list(fixtures)

finalSchedule = {}

week = 1
while len(fixtures) > 0:
    print(f'Week {week}')
    continueWeek = True
    matchCount=1
    week_fixtures = list(fixtures)
    while continueWeek == True:
        
        if week not in finalSchedule.keys():
            finalSchedule[week] = []
            
        
        #match = random.sample(week_fixtures, 1)[0]
        match = week_fixtures[0]
        
        finalSchedule[week].append(match)
        
        # Remove from fixtures
        fixtures.remove(match)
        week_fixtures.remove(match)
        
        # Remove those 2 teams from possible week
        team1, team2 = match.split(' - ')
        
        week_fixtures_removed = [x for x in week_fixtures if team1 in x or team2 in x]
        week_fixtures = [x for x in week_fixtures if team1 not in x and team2 not in x]
        print(f'\tMatch {matchCount}: {match} \n\t\t - CAN NOT SCHEDULE: {week_fixtures_removed}')
        
        matchCount += 1
        if len(week_fixtures) == 0:
            week += 1
            continueWeek = False
Related