Round robin scheduling requirement: Games Per Team Total
I’m new to round robin scheduling and stuck on this requirement where we pass in the Teams array and minimum number of matches a team should play. I’ve implemented single and double round robin algorithm such as:
teams = [‘T1’, ‘T2’, ‘T3’, ‘T4’];
Single round robin generates this:
T3 vs T2
T4 vs T1
T2 vs T4
T3 vs T1
T1 vs T2
T3 vs T4
Double Round Robin Generates This:
e.g
count = len(teams)
adjusted_count = count - 1 if count % 2 == 0 else count
total_matches = adjusted_count * 2
T3 vs T4
T1 vs T2
T4 vs T1
T3 vs T2
T2 vs T4
T3 vs T1
T4 vs T3
T2 vs T1
T1 vs T4
T2 vs T3
T4 vs T2
T1 vs T3
Now, requirement is such as: I pass teams arra, I set number of minimum matches that each team should always play, e.g 4 Teams, Min matches per team total =3
How do I implement that?
My Code:
import random
def rotate(lst):
return lst[1:] + lst[:1]
def make_schedule(teams, rounds=None, shuffle=True, seed=None):
team_count = len(teams)
if team_count < 2:
return []
# Account for odd number of teams by adding a bye
if team_count % 2 == 1:
teams.append(None)
team_count += 1
if shuffle:
# Seed shuffle with seed or randomize if seed is None
random.seed(seed)
random.shuffle(teams)
elif seed is not None:
# Generate friendly notice that seed is set but shuffle is set to false
raise ValueError("Seed parameter has no effect when shuffle parameter is set to False")
half_team_count = team_count // 2
if rounds is None:
rounds = team_count - 1
schedule = []
for round in range(1, rounds + 1):
round_matches = []
for key in range(half_team_count):
team1 = teams[key]
team2 = teams[key + half_team_count]
# Home-away swapping
matchup = [team1, team2] if round % 2 == 1 else [team2, team1]
round_matches.append(matchup)
schedule.append(round_matches)
teams = rotate(teams)
return schedule