Andrew
Andrew

Reputation: 2549

Printing list permutation

I have (in C#) a list of objects (each one containing a list) and I'd like to print out every permutation of the items in the objects' lists. Each iteration would use one item from each list. What would be the most efficient way to achieve this? Pseudo code is welcomed.

public class Detail {

    public int type;
    public List<String> codes;

    public Detail(int i){

        this.type = i;
        this.codes = new List<String>();

    }

}

Later on...

List<Detail> ListOfDetail = new List<Detail>();
foreach(Field i in listBox.Items)
    ListOfDetail.Add(new Detail(i));

The code list, which is assigned from a db, can be anything from 2 to 250.

If I had 3 objects (A, B, and C), with A holding 1,2,3, B holding 4,5,6, and C holding 7,8,9, I'd want it to print the following:

147
148
149
157
158
159
167
etc...

Upvotes: 1

Views: 293

Answers (1)

Ken Pespisa
Ken Pespisa

Reputation: 22284

If I'm following you correctly, you want something like this:

UPDATED:

foreach(var secondaryList in primaryListOfObjects) {
    StringBuilder sb = new StringBuilder();
    foreach(var str in secondaryList) {
        sb.Append(str);
    }
    Console.WriteLine(sb.ToString());
}

Upvotes: 2

Related Questions