Reputation: 41
I am trying to write algorithm for football group stage. for example: i have 4 teams in the stage.
teams = ['team1', 'team2', 'team3', 'team4']
then i got an all unique pairs
import itertools
team_pairs = list(itertools.combinations(teams, 2))
my team_pairs are unique matches between teams
[('team1', 'team2'), ('team1', 'team3'), ('team1', 'team4'), ('team2', 'team3'), ('team2', 'team4'), ('team3', 'team4')]
and now. how can i create all rounds? for example:
('team1', 'team2') ('team3', 'team4')
('team1', 'team3') ('team2', 'team4')
('team1', 'team4') ('team2', 'team3')
and how to do that for 6 teams or for 7 teams? please help!!!
mb i explained bad:
i have an 11 teams. Then i break them into groups. and i have:
in each group, the team must play with all teams in group. lets take 1 group, teams are:
teams = ['team1', 'team2', 'team3', 'team4']
win - 3 points draft 1 point loose - 0 points
they can't play everybody at one time. they are playing 3 days.
* first day - team1 vs team2 and team3 vs team4
* second day - team1 vs team3 and team2 vs team4
* third day - team1 vs team4 and team2 vs team3
then i can summ points.
But i don't actually understand how to split my group (my team_pairs) by days and what team play in what day.
Upvotes: 3
Views: 2563
Reputation: 29126
The problem can be seen as an exact cover problem and can be solved like a Sudoku with Algorithm X, of which there are Python implementations available on the web.
The set that needs to be covered consists of:
which for four teams is:
A1, A2, A3, B1, B2, B3, C1, C2, C3, D1, D2, D3, AB, AC, AD, BC, BD, CD
where B3
means team B
plays on day 3
and BD
means team B
plays team D
.
The available subsets are all match pairings combined with all match days, which for four teams is:
AB1: A1, B1, AB
AB2: A2, B2, AB
AB3: A3, B3, AB
AC1: A1, C1, AC
...
CD3: C3, D3, CD
Solving this yields a great many possible fixtures, essentially permutations of teams and match days. Pick one, order by match day and play.
There are no solutions if there is an odd number of teams. Add a null team as dummy and don't play the matches where one of the sides is the dummy.
Upvotes: 3