I thought this was a pretty cool math problem, but I wanted to visualize the actual process so I wrote this for anyone that wants to test out what Servy (well, Eric Lippert) wrote:
int length = 4;
var lists = new List<List<string>>();
var random = new Random(1234);
for(int i = 0; i < length; i++)
{
var inLength = random.Next(4, 8);
var tempList = new List<string();
for (int j = 0; j < inLength; j++_
{
tempList.Add(string.Format("{{String Coords: {0}, {1}}}", i, j));
}
lists.Add(tempList);
}
var cp= lists.CartesianProduct();
var output = RenderString(cp);
and RenderString
:
private static string RenderString(IEnumerable<IEnumerable<string>> cp)
{
var sb = new StringBuilder();
foreach (var item in cp)
{
sb.AppendLine(item.Aggregate((a, b) => a + b));
}
return sb.ToString();
}
This will give you an output
that looks like
{String Coords: 0, 0}{String Coords: 1, 0}{String Coords: 2, 0}{String Coords: 3, 0}
{String Coords: 0, 0}{String Coords: 1, 0}{String Coords: 2, 0}{String Coords: 3, 1}
{String Coords: 0, 0}{String Coords: 1, 0}{String Coords: 2, 0}{String Coords: 3, 2}
{String Coords: 0, 0}{String Coords: 1, 0}{String Coords: 2, 0}{String Coords: 3, 3}
{String Coords: 0, 0}{String Coords: 1, 0}{String Coords: 2, 0}{String Coords: 3, 4}
{String Coords: 0, 0}{String Coords: 1, 0}{String Coords: 2, 1}{String Coords: 3, 0}
...
{String Coords: 0, 4}{String Coords: 1, 4}{String Coords: 2, 6}{String Coords: 3, 4}
Pretty cool if you want to visualize what is going on.