The list problem

jjenus

JJenus

Posted on September 22, 2022

The list problem

I've been working on this algorithm since I learned how to write arrays in C, 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

  • A team can't play twice in a match week (20 teams can only make 10 matches),
  • 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, and a total of only 320 fixtures is selected.

Observation

Over the years I've tried different implementations in different languages, ranging from java, python, php, javascript, and c with no solution.
Then I decided to shuffle the fixtures and the selected fixtures were in a range of 360 to 369 in 10 different runs. Why the change of heart?

Is this a list problem or there is simply a better way?

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
#Shuffle this to see the change
fixtures =  [f"{home} - {away}" for home in teams for away in teams if home != away]

#38 match weeks if 10 matches are played every week. 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 season, but CHE - ARS only
s_fixtures = []

#Making fixtures for all 38 weeks
for i in range(38):
    match_week = [] #should be exactly 10 unique 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)
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
jjenus
JJenus

Posted on September 22, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

The list problem
programming The list problem

September 22, 2022