find all possible combinations in a List < List < int > > [modified]
-
can't figure out a lambada expression that would yield a falttened result of all combinations within the input list of lists
List < List < int > > FindEm(List < List < int > > inputLists) {
var X = ?!?
}assuming that the input was populated with
List A = new List(new int\[\] { 1, 2, 3 }); List A = new List(new int\[\] { 4, 5, 6 }); List A = new List(new int\[\] { 7, 8, 9 }); List\> lists = new List\>( ); lists.Add(A); lists.Add(B); lists.Add(C);
the result would look something like 1,4,7 1,4,8 1,4,9 2,4,7 ..... any help or direction would be greatly appriciated :)
modified on Monday, October 5, 2009 1:35 PM
-
can't figure out a lambada expression that would yield a falttened result of all combinations within the input list of lists
List < List < int > > FindEm(List < List < int > > inputLists) {
var X = ?!?
}assuming that the input was populated with
List A = new List(new int\[\] { 1, 2, 3 }); List A = new List(new int\[\] { 4, 5, 6 }); List A = new List(new int\[\] { 7, 8, 9 }); List\> lists = new List\>( ); lists.Add(A); lists.Add(B); lists.Add(C);
the result would look something like 1,4,7 1,4,8 1,4,9 2,4,7 ..... any help or direction would be greatly appriciated :)
modified on Monday, October 5, 2009 1:35 PM
cechode wrote:
a lambada expression
you'll need quite a swing to pull this one off. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
-
cechode wrote:
a lambada expression
you'll need quite a swing to pull this one off. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
i've been swingin and missing for a bit now. now trying to somehow get
among the things i've tried so far
tried to convert this to a self calling kinda thing
var PP = A.Where(x = > x > 0).SelectMany(g = > B.Where(c = > c > 0).Select(c = > new { aa = g, bb = c })).ToList();but to no avail
then tried to get a bunch of from clauses ( one for each inner list )
var ttt = (from g in A from c in B select new { aa = g, bb = c }).ToList();
but also failed
now i figured i'd ask the experts here
-
i've been swingin and missing for a bit now. now trying to somehow get
among the things i've tried so far
tried to convert this to a self calling kinda thing
var PP = A.Where(x = > x > 0).SelectMany(g = > B.Where(c = > c > 0).Select(c = > new { aa = g, bb = c })).ToList();but to no avail
then tried to get a bunch of from clauses ( one for each inner list )
var ttt = (from g in A from c in B select new { aa = g, bb = c }).ToList();
but also failed
now i figured i'd ask the experts here
Ok... As a warning, my brain is a little lopsided today... Here's the first weird idea that popped into my head... Partly in pseudocode, and haven't tried it out, but here ya go...
private IEnumerable<List<int>> Test(List<List<int>> data)
{
int numDigits = data.Count;
int lastCount = data[numDigits - 1].Count;
int[] indices = new int[data.Count];
while (indices[numDigits - 1] < lastCount)
{
//yield return a list composed of data[0][indices[0]], data[1][indices[1]], ...indices\[0\]++; for (int idx = 0; idx < numDigits - 1; idx++) if (indices\[idx\] == data\[idx\].Count) { indices\[idx\] = 0; indices\[idx + 1\]++; } }
}
Bit of a different angle from what you're trying... Instead of going for recursion, think of it like a series of digits. What this really does is count through the following sequence (Assuming all of your arrays have 3 digits): 000, 100, 200, 010, 110, 210, 020, 120, 220, 001, 101, 201... etc And it forms results such that, for example, 120 = the second, third, and first digits of the elements, respectively... Anyway, you get the idea. If you know for a fact that you'll be dealing with 3-digit inner lists, you can simplify this a bit...
Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)