Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to code for dynamic for-loop levels?

My problem is like this:

I have several lists need to be permuted, but the list numbers are unknowable. And every element numbers in every list are also unknowable. Sicne I would like to traverse all list element combination, like 1) pick A from list 1, A from list 2, A from list 3; 2) ick A from list 1, A from list 2, B from list 3 ... for ALL permutation.

I use nested for-loop to traverse, like if I have two lists, then:

        for (int i = 0; i < list[0].EnergyParameters.ListEnergyLevelCandidates.Count; i++)
        {
            for (int j = 0; j < list[1].EnergyParameters.ListEnergyLevelCandidates.Count; j++)
            {
                // Do sth
            }
        }

If I have three lists, then:

        for (int i = 0; i < list[0].EnergyParameters.ListEnergyLevelCandidates.Count; i++)
        {
            for (int j = 0; j < list[1].EnergyParameters.ListEnergyLevelCandidates.Count; j++)
            {
                for (int k = 0; k < list[2].EnergyParameters.ListEnergyLevelCandidates.Count; k++)
                {
                    // Do sth
                }
            }
        }

Because the list numbers are unknowable, so the nest numbers are unknowable, which means, I don't know how many levels of for-loop needs to be written.

Under this kind of circumstance, how can I write code for dynamic for-loop levels? I don't want to write 10 loops for 10 lists.

like image 781
Asker Avatar asked May 21 '12 22:05

Asker


1 Answers

In case you do not know how many lists there are, you do not write nested loops: instead, you write recursion. At each level of the invocation you loop a single list, like this:

void AllCombos(List<string>[] lists, int level, string[] current) {
    if (level == lists.Length) {
        // Do somthing; items of current[] contain elements of the combination
    } else {
        foreach (var s in lists[level]) {
            current[level] = s;
             AllCombos(lists, level+1, current);
        }
    }
}

Call AllCombos as follows:

var lists = new List<string>[10];
for (int i = 0 ; i != 10 ; i++) {
    lists[i] = PopulateMyList(i);
}
string[] current = new string[lists.Length];
AllCombos(lists, 0, current);
like image 172
Sergey Kalinichenko Avatar answered Oct 24 '22 02:10

Sergey Kalinichenko