setcombinationspermutationarrange-act-assert

Listing all sets of 5 teams that has one element of each rank


This will be used for a fairly-ranked team-picking (each team must have exactly one element of each rank). One example of elegibe players would be:

Rank 1:'Liam', 'Emma', 'Noah', 'Olivia', 'Ava'
Rank 2:'Ethan', 'Sophia', 'Isabella', 'Jackson', 'Mia'
Rank 3:'Aiden', 'Amelia', 'Lucas', 'Harper', 'Mason'
Rank 4:'Ella', 'Logan', 'Grace', 'James', 'Lily'
Rank 5:'Benjamin', 'Chloe', 'Jacob', 'Scarlett', 'Michael'

And one of the many example of sets of teams would be:

Team 1: 'Liam','Ethan','Aiden','Ella','Benjamin'
Team 2: 'Emma','Sophia','Amelia','Logan','Chloe'
Team 3: 'Noah','Isabella','Lucas','Grace','Jacob'
Team 4: 'Olivia','Jackson','Harper','James','Scarlett'
Team 5: 'Ava','Mia','Mason','Lily','Michael'

How can I generate every possibile sets of teams?

I tried using Python, but can only get one set of teams, but not EVERY possible sets.


Solution

  • This code generates all 207,360,000 different combinations of teams. The idea is that we fix the rank-1 people as the "leaders" of the team, and then for each other rank, we consider all permutations of the people of that rank and assign them to teams based on the permutation.

    import itertools
    
    def teams(ranks):
        choices = [[ranks[0]]]
        for r in ranks[1:]:
            choices.append(list(itertools.permutations(r)))
    
        for p in itertools.product(*choices):
            yield list(zip(*p))
    
    ranks = [
        ['Liam', 'Emma', 'Noah', 'Olivia', 'Ava',],
        ['Ethan', 'Sophia', 'Isabella', 'Jackson', 'Mia',],
        ['Aiden', 'Amelia', 'Lucas', 'Harper', 'Mason'],
        ['Ella', 'Logan', 'Grace', 'James', 'Lily'],
        ['Benjamin', 'Chloe', 'Jacob', 'Scarlett', 'Michael'],
    ]
    
    for ts in teams(ranks):
        for i, t in enumerate(ts):
            print('team %d: %s' % (i, ' '.join(['%-8s' % x for x in t])))
        print()