// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; namespace E { public class Permuting { public delegate void Processor(string s); public static void Print(string s) { Console.WriteLine(s); } private static void PermuteStrings(string prefix, int ix, List[] data, Processor p) { foreach(string s in data[ix]) { if(ix >= data.Length - 1) { p(prefix + s); } else { PermuteStrings(prefix + s, ix + 1, data, p); } } } public static void PermuteLists(List> data, List[] temp, int ix, bool[] used, Processor p) { if(ix >= temp.Length) { PermuteStrings("", 0, temp, p); } else { for(int i = 0; i < temp.Length; i++) { if(!used[i]) { used[i] = true; temp[ix] = data[i]; PermuteLists(data, temp, ix + 1, used, p); used[i] = false; } } } } public static void PermuteLists(List> data, Processor p) { List[] temp = new List[data.Count]; bool[] used = new bool[data.Count]; PermuteLists(data, temp, 0, used, p); } public static void Main(string[] args) { List> data = new List>(); data.Add(new List()); data.Add(new List()); data.Add(new List()); data[0].Add("A"); data[0].Add("B"); data[0].Add("C"); data[0].Add("D"); data[1].Add("X"); data[1].Add("Y"); data[1].Add("Z"); data[2].Add("1"); data[2].Add("2"); PermuteLists(data, Print); } } }