-1

I'm trying to create a program which computes X Choose Y and creates a list of possible combinations (order does not matter, no repetition).

So for example, A, B, C, and D choose 2: AB, AC, AD, BC, BD, CD would be generated and stored.

Each letter represents a chemical reagent in a test tube. A resides in Tube 1, B in Tube 2, and so on.

A procedure would then create a CSV or text file where it would read each combination and store it as a line.

For example, it would read AB and convert it to Tube 1, Tube 2

Read AC and convert it to Tube 1, Tube 3

and so on.

What would be the most efficient way to generate the list of combinations, store it, and then read it to create a text/CSV file?

Thanks.

user3633260
  • 35
  • 1
  • 3
  • 8
  • 1
    To be honest I wouldn't be surprised if this was closed as "too broad" You have multiple questions here: 1. Most efficient way to generate combinations 2. Most efficient way to store a list 3. Most efficient way to read a list 4. Most efficient way to create a text/CSV file. It might benefit you to break this problem down and look up how to do each of those individual parts, as I expect each individual part has been answered already. – awksp Jun 13 '14 at 20:05
  • For the combinatorics bit, you can use apache commons-math3 `CombinatoricsUtils#combinationsIterator`: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/util/CombinatoricsUtils.html – azurefrog Jun 13 '14 at 20:09
  • A good answer to your combinatorics question might be found at http://stackoverflow.com/q/12013584/509840. And an answer for both reading and writing CSV files may be found at http://stackoverflow.com/q/101100/509840. – rajah9 Jun 13 '14 at 20:14

1 Answers1

1

As someone pointed out in the comments, you can use this class to calculate the combinations. http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/util/CombinatoricsUtils.html

Take a look at the combinationsIterator. It generates the k-sized subsets from the first n integers.

Using that in your problem, if you want A, B, C, D choose 2, you would call

ArrayList<Integer[]> combinations = new ArrayList();
Iterator iter = combinationsIterator(4, 2);
while (iter.hasNext()) {
    int[] resultint = iter.next();
    Integer[] resultInteger = new Integer[2];
    for (int i = 0; i < 2; i++) {
        resultInteger[i] = Integer.valueOf(resultint[i]);
    }
    combinations.add(resultInteger);
}

You would need to associate the integers with your tube and reactant names and you can do that easily through an associative array, enumeration, or something similar.

To write to a file, I would recommend using a BufferedWriter. You would write it doing something like this

BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"));
for (Integer[] i : combinations) {
    out.writeline(i);
}
out.close();

If you want to use csv, make sure that you format your text to have commas and to follow the standardized format for csv. You would also change your output file name to "out.csv". If you want to guarantee order, use a for loop instead of a for-each loop.

jluckin
  • 662
  • 4
  • 6