0

I have a list of N players (let's say, 14 players - 1 to 14 numbers in the examples).

I want to obtain a Collection of ALL possible TUPLE of Y teams made by X players. There can be players left out, of course.

Input example: N = 14 players, Y = 3 teams, each composed by X = 3 players. That means 14 players, split into 3 teams of 3 players. I want all the possibilities.

1) [123],[456],[789] 10,11,12,13,14 left out.

2) [456],[789],[10,11,12] 1,2,3,13,14 left out. 3) ....

I already looked into this post, which helped me to obtain all possible teams of X players, which i think might help me to reach my goal: What is the best way to find all combinations of items in an array? Now that i have all possible teams of X players, i need to get a step further. When i'll have all the possible tuple of teams, i will select which combination best suits my needs sorting them. Can you guys help me? C# code would be great but even pseudocode can help.

Fabio
  • 48
  • 1
  • 9
  • I am not clear what you are asking for help with. Perhaps if you provided some code and pointed out where you need what exact outcome. – John Wu Feb 22 '18 at 22:09
  • 2
    Note your sample problem has 7,971,964 possible team sets. – NetMage Feb 22 '18 at 22:29
  • @JohnWu i wrote it down as better as i could, my english is not top notch i am afraid. But i got the answer i needed already, so it's fine! – Fabio Feb 23 '18 at 13:52
  • 1
    @JohnWu I found it very clear. Too many questions being closed as unclear these days. – NetMage Feb 23 '18 at 18:23

1 Answers1

1

I think you want combinations, not permuatations. Permutations means the order of players matters.

Using an extension method you can generate the combinations:

public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> elements, int k) {
    return k == 0 ? new[] { new T[0] } :
      elements.SelectMany((e, i) =>
        elements.Skip(i + 1).Combinations(k - 1).Select(c => (new[] { e }).Concat(c)));
}

So, for your sample problem, setup the parameters:

var N = 14;
var NumTeams = 3;
var NumPlayersPerTeam = 3;

First you can generate all the players:

var players = Enumerable.Range(1, N);

Then you can get every possible team, by combining the players NumPlayersPerTeam at a time:

var AllTeams = players.Combinations(NumPlayersPerTeam);

Then you can get all the sets of teams, by combining the teams NumTeams at a time:

var PossibleTeamSets = AllTeams.Combinations(NumTeams);
NetMage
  • 26,163
  • 3
  • 34
  • 55
  • This is EXACTLY what i needed, glad you understood immediately. Your suggestion was correct and elegant but also so simple i felt a bit stupid after reading it. Thanks a lot, this was really helpful. – Fabio Feb 23 '18 at 13:55