I am unsing c#..
I have an araay of arrays, can grow to 30.
char[][] myArray = new char[][]{
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' },
new char[] { '3', '2', '1' }
};
Want to create all combinations in format
"3333333333333333 3333333333333332 3333333333333331 3333333333333323 3333333333333322 3333333333333321 3333333333333313 3333333333333312 3333333333333311 3333333333333233 3333333333333232 ..........."
I applied two approaches, First:
var data=myArray.Aggregate((IEnumerable<string>)new[] { string.Empty }, (e, d) => e.SelectMany(_ => d, (a, b) => a + b)).ToList();
File.AppendAllText(@$"C:\test.txt", (String.Join(Environment.NewLine,data)));
In this appraoch, can generate sequences pretty quick as long as array count is 16. Above 16, I run into memory issue.
Second approach is
var positions = from list1 in mainList
from list2 in mainList
from list3 in mainList
from list4 in mainList
......
select new
{
list1,
list2,
list3,
....
Then use Parallel for loop using 32 threads. In second appraich, after running over 24 hours the loop is still running.
Is there another way to to do this faster wihtout running into memory issues...
You basically have 1, 2 and 3 as possible values. So they very much look like digits of a number. Let's create an initial array to have the state stored somewhere:
and let's create an array of values:
Now, let's do a loop:
We are basically "incrementing" a 3-base number where the array elements represent the digits.
EDIT
Concerns were expressed regarding
IndexOfin the comment section. This is an implementation withoutIndexOf:But, if we choose this approach, we will need to store the indexes in
myArrayrather than the actual values to be able to compute things based on them withoutIndexOfand hence we need to keep this in mind when we display the values too.