循环赛安排要求:每支球队的比赛总数
我是循环赛安排的新手,并且坚持这个要求,我们在团队数组中传递以及球队应该参加的最小比赛数。我已经实现了单循环算法和双循环算法,例如:
teams = ['T1', 'T2', 'T3', 'T4'];
单循环生成此:
T3 vs T2
T4 vs T1
T2 vs T4
T3 vs T1
T1 vs T2
T3 vs T4
双循环生成此:
例如
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
现在,要求如下:我传递团队数组,我设置了每支球队应始终参加的最少比赛数量,例如 4 支球队,每支球队的最少比赛总数 =3
我如何实现这一点?
我的代码:
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
此代码基于以下内容创建了对战格式提供的轮次。但我想通过传递 minMatchesPer team 来修改它,例如 3 场比赛。一旦每支球队达到至少 3 场比赛,循环就会中断。我给出基本函数只是为了更好地理解。
import random
def rotate(lst):
return lst[1:] + lst[:1]
def make_schedule(teams, min_matches_per_team, shuffle=True, seed=None):
team_count = len(teams)
if team_count < 2:
return []
if shuffle:
random.seed(seed)
random.shuffle(teams)
elif seed is not None:
raise ValueError("Seed parameter has no effect when shuffle parameter is set to False")
half_team_count = team_count // 2
schedule = []
matches_played = {team: 0 for team in teams} # Keep track of matches played by each team
round = 1
while any(count < min_matches_per_team for count in matches_played.values()):
round_matches = []
for key in range(half_team_count):
team1 = teams[key]
team2 = teams[key + half_team_count]
# Check if both teams have played less than the minimum required matches
if matches_played[team1] < min_matches_per_team and matches_played[team2] < min_matches_per_team:
matchup = [team1, team2] if round % 2 == 1 else [team2, team1]
round_matches.append(matchup)
matches_played[team1] += 1
matches_played[team2] += 1
if round_matches: # Only append if there are matches in the round
schedule.append(round_matches)
round += 1
teams = rotate(teams)
return schedule
# Example usage
teams = ['T1', 'T2', 'T3', 'T4']
min_matches_per_team = 3
schedule = make_schedule(teams, min_matches_per_team, shuffle=True, seed=42)
for i, round_matches in enumerate(schedule):
print(f"Round {i+1}:")
for match in round_matches:
print(f" {match[0]} vs {match[1]}")
Explanation:
- Track Matches Played:
-
We introduce a dictionary
matches_played
to keep track of how many matches each team has played so far. -
Loop Until Condition Met:
-
Instead of a fixed number of rounds, we use a
while
loop that continues until all teams have played at leastmin_matches_per_team
. -
Conditional Match Scheduling:
-
Inside the loop, we only schedule a match between
team1
andteam2
if both teams have played fewer thanmin_matches_per_team
matches. -
Update Matches Played:
-
After scheduling a match, we increment the match counts for both participating teams in the
matches_played
dictionary. -
Round Management:
-
We only increment the round counter and append
round_matches
to theschedule
if there were any matches scheduled in that round. This ensures we don't add empty rounds.
Now, the code will generate a schedule where each team plays at least the specified minimum number of matches, even if it means having fewer total rounds or some teams playing slightly more than the minimum.
标签:python,scheduled-tasks,scheduling,round-robin,tournament From: 78810255