You are organizing a football tournament. The tournament is held in the all-play-all format, that is, each team must play each team exactly 1 time.
For example, if there are 4 teams in the tournament (let’s denote them A, B, C, D), then the game scheme will look like this:
Round 1: AB, CD
Round 2: AC, BD
Round 3: AD, BC
Thus, the tournament will consist of three rounds.
It is necessary to implement a function that gets the number of teams (always a positive and even number) and returns the schedule of games for each round in the form of a matrix.
Each row of the matrix represents one round. Each column of the matrix represents a pair of playing teams. The match is presented as an array with two teams. The names of the teams are given in numbers from 1 to n, where n is the number of teams.
Sample Input:
2
Sample Output:
[[(1, 2)]]
**AND WE HAVE THESE CODE, THAT IS NOT WORKING **
def build_matches_table(t):
teams = list(range(1, t + 1))
ans = []
for round_num in range(t - 1):
round_pairs = []
for i in range(t // 2):
round_pairs.append((teams[i], teams[t - i - 1]))
ans.append(round_pairs)
teams = [teams[0]] + teams[2:] + [teams[1]]
return ans
t = int(input())
if t % 2 != 0:
print()
else:
matches_table = build_matches_table(t)
print("[")
for round_pairs in matches_table:
print(f" {round_pairs},")
print("]")
we tried these code too and it still didn’t worked because is’s needs to be like a matrix
def round_robin_schedule(num_teams):
# If odd number of teams, add a dummy team (represented by 0)
if num_teams % 2 != 0:
num_teams += 1
teams = list(range(1, num_teams)) + [0]
else:
teams = list(range(1, num_teams + 1))
num_rounds = num_teams - 1
schedule = []
for round_num in range(num_rounds):
round_matches = []
for i in range(len(teams) // 2):
team1 = teams[i]
team2 = teams[-1 - i]
if team1 != 0 and team2 != 0:
round_matches.append((team1, team2))
schedule.append(round_matches)
# Rotate teams, except for the first one
teams = [teams[0]] + teams[-1:] + teams[1:-1]
return schedule
schedule = int(input())
print(schedule)
1