I have 5 text files, which contain some elements.
Example of contents inside A:
9864902,9864892,9864891,9864890,9864889,9864888,9864887,9864886,9864885,9864884,9864883,9864882,9864881,9864880,9864879,9864878,9864877,9864876,9864873,9864872,9864870,9864869,9864868,9864867,9864866,9864865,9864864,9864863,9864862,9864861,9864858,9864857,9864856,9864855,9864854,9864853,9864852,9864851,9864850,9864849,9864848,9864847,9864846,9864845,9864844,9864843,9864842,9864841,9864840,9864839,9864838,9864837,9864836,9864835,9864834,9864833,9864832,9864831,9864830,9864829,9864828,9864827,9864826,9864825,9864824,9864823,9864822,9864821,9864820,9864819,9864818,9864817,9864816,9864815,9864814,9864813,9864812,9864811,9864810,9864809,9864808,9864807,9864806,9864805,9864804,9864803,9864802,9864801,9864800,9864799,9864798,9864797,9864796,9864795,9864794,9864793,9864792,9864791,9864790,9864789,9864788,9864787,9864786,9864785,9864784,9864783,9864782,9864781,9864780,9864779,9864778,9864777,9864776,9864775,9864774,9864773,9864772,9864771,9864770,9864769,9864768,9864767,9864766,9864765,9864764,9864763,9864762,9864761
(and many many more, about 18k, but now I wish to get 1st 1000 only.)
here is one of the method to get contents inside text file.
public string getA(int index)
{
// Contains 1000 elements
string[] Array_A = File.ReadAllLines("D:\\user\\A.txt");
string[] A = null;
for (int i = 0; i < Array_A.Length; i++)
{
A = Array_A[i].Split(',');
}
return A[index].ToString();
}
Other methods are same writing as above. Just change the 'A' to other name.
Now I want to write another method to get elements which combining all of elements in text files.
public string[] UniqueCombination(int number)
{
// Write the code here
}
For example, UniqueCombination(7), I need to get 7 unique combinations.
A(0) B(0) C(0) D(0) E(0)
A(0) B(0) C(0) D(1) E(0)
A(0) B(0) C(1) D(0) E(0)
A(0) B(0) C(1) D(1) E(0)
A(0) B(0) C(2) D(0) E(0)
A(0) B(0) C(2) D(1) E(0)
A(0) B(3) C(0) D(0) E(0)
(Above is the example of combinations, because E just has only 1 element, so always return first position element.)
However, for this case, I would like to get thousands, might up to 5 digits(20,000) combinations. How to do that? I have already wrote another method for random index. But for this case I have no idea.
(p.s the sequence of the elements are doesn't matter, just to promise every combination is unique)
int size = 7;
// read the text and create lists for each file
var listA = ReadFileToList("D:\\user\\A.txt");
var listB = ReadFileToList("D:\\user\\B.txt");
var listC = ReadFileToList("D:\\user\\C.txt");
var listD = ReadFileToList("D:\\user\\D.txt");
// get the combinations from lists
var combinations = (from a in listA
from b in listB
from c in listC
from d in listD
select new[] { a, b, c, d })
.Take(size)
.ToList();
if you need randomly select items
var randomCombinations = (from a in listA
from b in listB
from c in listC
from d in listD
select new[] { a, b, c, d })
.OrderBy(n => Guid.NewGuid())
.Take(size)
.ToList();
You need helper method like below
public List<string> ReadFileToList(string path)
{
List<string> temp = new List<string>();
using (StreamReader r = new StreamReader(path))
{
string line;
if ((line = r.ReadLine()) != null)
{
temp =line.Split(',').ToList();
}
}
return temp;
}
you can print result as below
Console.WriteLine(string.Join("|",
randomCombinations.Select(c => string.Join(",", c))));
Result will show something like below
A,D,E,G|A,D,F,G|B,D,F,G|B,C,E,G|A,C,E,G|A,C,F,G|B,C,F,G
OR Extending your code as below
foreach (var item in combinations) { Console.WriteLine(string.Join(",", item)); }