I have a method that generates matches for each best of two teams from group stage.
Group A
T1───┐
│
T2───┘
├───┐
T3───┐ │ ├───T1
│ │ │
T4───┘ │ ├───T6
Group B ├───│
T5───┐ │ ├───T2
│ │ │
T6───┘ │ ├───T5
├───┘
T7───┐
│
T8───┘
def generate_final_stage(advanced_teams):
teams_from_group_stage = advanced_teams
matches = []
for i in range(len(teams_from_group_stage)):
teams = []
if i != len(teams_from_group_stage) - 1:
team_1 = teams_from_group_stage[i][0]
team_2 = teams_from_group_stage[i + 1][1]
teams.append(team_1)
teams.append(team_2)
else:
team_1 = teams_from_group_stage[i][0]
team_2 = teams_from_group_stage[0][1]
teams.append(team_1)
teams.append(team_2)
matches.append([teams[0], teams[1]])
return matches
def main():
# Possible Inputs
advanced_teams = [[1, 2, 3], [4, 5, 6]]
advanced_teams2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
advanced_teams3 = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
schedule = generate_final_stage(advanced_teams3)
print(schedule)
if __name__ == "__main__":
main()
I would like to improve this script so that it also generates matches for subsequent places. If 3, 4, 5 or more teams go to PlayOff, this script should generate the games accordingly. If the number of teams from the group phase is not odd, games must be generated as follows: Best team vs. worst team. For example:
1st. Team from Group A vs. 2nd Team from Group B
1st. Team from Group B vs. 2nd Team from Group A
when 3 teams go from the group stage to the PlayOff.
3rd. Team from Group A vs. 3rd Team from Group B
when 4 teams go from the group stage to the PlayOff.
3rd. Team from Group A vs. 4th Team from Group B
3st. Team from Group B vs. 4th Team from Group A
when 5 teams go from the group stage to the PlayOff.
5th. Team from Group A vs. 5th Team from Group B
and so on
For example from inpunt of [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
I expect following output:
[[1, 7], [6, 2], [3, 9], [4, 8], [5, 10]]
The amount of groups is also dynamic there can be 2, 4, 6, 8, 10 and so on groups. In this case first two groups plays with each other and next two the same and next and next
[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]]
The output should be:
[(1, 8), (7, 2), (3, 10), (9, 4), (5, 12), (11, 6), (13, 20), (14, 19), (15, 22), (16, 21), (17, 24), (18, 23)]
You can use recursion to get the desired result in case of N-teams¹ in M-groups²:
def playoff2g(g1, g2, r):
""" Get matches for 2 groups """
if len(g1) > 1:
r.extend([(g1[0], g2[1]), (g2[0], g1[1])])
playoff2g(g1[2:], g2[2:], r)
elif len(g1) == 1:
r.append((g1[0], g2[0]))
return r
def playoff(gs):
""" Get matches for multiple number of groups """
res = []
for i in range(0, len(gs)-1, 2):
res = playoff2g(gs[i], gs[i+1], res)
return res
groups = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]]
result = playoff(groups)
Output:
[(1, 8), (7, 2), (3, 10), (9, 4), (5, 12), (11, 6), (13, 20), (19, 14), (15, 22), (21, 16), (17, 24), (23, 18)]
¹ number of teams should be the same in all groups
² number of groups should be even (2,4,6...)