-1

I have a List<TeamName>, where class TeamName contains the List<MembersName>

So I have something like this:

List<TeamName> teamNameList //each item of "tnl" has public member "memberNameList"

...

class TeamName //each team has different member
{
   public TName  
   public List<MemberName> memberNameLis;
} 

class MemberName
{
     public MName;
}

Now i want to make all possible combination of MemberName from each Team.

e.g. I have 3 teams (T1, T2, T3) they have members count as -

T1 = 3 members, T2 = 2 Members, T3 = 1 member,

So now I need something -

T1M1 T2M1 T3M1

T1M1 T2M2 T3M1

T1M2 T2M1 T3M1

T1M2 T2M2 T3M1

T1M3 T2M1 T3M1

T1M3 T2M2 T3M1

List<List<MemberName>> combi;

how to do this in Code? i am just a beginner so kindly pardon me for not asking a question in a clean pattern. help please :)

Raihan Al-Mamun
  • 337
  • 6
  • 11
  • Combination: T1M1 T2M1 T3M1 T1M1 T2M2 T3M1 T1M2 T2M1 T3M1 T1M2 T2M2 T3M1 T1M3 T2M1 T3M1 T1M3 T2M2 T3M1 – Raihan Al-Mamun Apr 03 '17 at 10:46
  • no, both question is way different. each team has different list of Members – Raihan Al-Mamun Apr 03 '17 at 11:26
  • @RaihanAl-maMun, No I think its cartesian product only. Let me post the answer. – Deepak Sharma Apr 03 '17 at 11:58
  • The original question proposed as a duplicate has an extra layer of complexity, making it more difficult to see its applicability here. But it is still basically the same question. You just want a Cartesian product of your individual team lists. The above-marked duplicate represents the same basic problem in a similar way, and Eric's answer there references the previously-proposed duplicate for a more detailed solution. More generally, there are _lots_ of "Cartesian product" Q&A on Stack Overflow, so if you are having trouble understanding those two, just look for the others. – Peter Duniho Apr 03 '17 at 18:18

2 Answers2

1

lets say you have your team list as -

List<TeamName> tnl;
...
...
...

// now you can call like - 
IEnumerable<IEnumerable<MemberName>> allPossible = CartesianProduct(tnl.Select(c => c.mnl));
...
...

And Cartesian Product method will be the same

IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
{
    IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
    return sequences.Aggregate(
        emptyProduct,
        (accumulator, sequence) =>
            from accseq in accumulator
            from item in sequence
            select accseq.Concat(new[] { item })
        );
}

Try it, for your input it will give you your required result.

if you will loop -

 foreach (var item in r4)
 {
     foreach (var mn in item)
     {
         Console.Write("\t"+mn.Name);
     }
     Console.WriteLine();
 }

you will get your answer -

T1M1    T2M1    T3M1
T1M1    T2M2    T3M1
T1M2    T2M1    T3M1
T1M2    T2M2    T3M1
T1M3    T2M1    T3M1
T1M3    T2M2    T3M1

I have tested it.

Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31
0

Here is the solution

List<TeamName> teamNameList = new List<TeamName>();
teamNameList.Add(new TeamName() { TName = "T1",
    memberNameList = new List<MemberName> {
        new MemberName { MName = "M1" },
        new MemberName { MName = "M2" },
        new MemberName { MName = "M3" } } });
teamNameList.Add(new TeamName() { TName = "T2",
    memberNameList = new List<MemberName> {
        new MemberName { MName = "M1" },
        new MemberName { MName = "M2" } } });
teamNameList.Add(new TeamName() { TName = "T3",
    memberNameList = new List<MemberName> { new MemberName { MName = "M1" } } });

List<List<MemberName>> combi = new List<List<MemberName>>();
//indexes keep index of each member for each team
List<int> indexes = new List<int>(teamNameList.Count);
//initialize them to the first members
for (int i = 0; i < teamNameList.Count; i++)
    indexes.Add(0);

//We will enumerate all combinations in this loop
while (true)
{
    //Create a new entry for combi
    List<MemberName> members = new List<MemberName>();
    //Select respective member based on the current index value for each team
    for (int i = 0; i < teamNameList.Count; i++)
        members.Add(new MemberName() { MName = teamNameList[i].TName + teamNameList[i].memberNameList[indexes[i]].MName });

    combi.Add(members);

    //Increment indexes and go to the next combination
    for (int i = indexes.Count - 1; i >= 0; i--)
    {
        indexes[i]++;
        //If we enumerated all members of the current team - start from the beginning of that members
        //Else we should proceed with the next member
        if (indexes[i] == teamNameList[i].memberNameList.Count)
            indexes[i] = 0;
        else
            break;
    }

    //If we enumerated all combinations than all indexes will become zero because of the previous loop
    if (indexes.Sum() == 0)
        break;
}
Ilya Ivanov
  • 123
  • 6