Permutations { public static List<int[]> getAllBinaryCombinations(int numElements) { List<int[]> output = new ArrayList<>(); output.add(new int[] {0}); output.add(new int[] {1}); for (int elementId = 1; elementId < numElements; elementId++) { List<int[]> newOutput = new ArrayList<>(); for (int[] instance : output) { int[] newInstance0 = new int[instance.length + 1]; int[] newInstance1 = new int[instance.length + 1]; for (int i = 0; i < instance.length; i++) { newInstance0[i] = instance[i]; newInstance1[i] = instance[i]; } newInstance0[elementId] = 0; newOutput.add(newInstance0); newInstance1[elementId] = 1; newOutput.add(newInstance1); } output = newOutput; } return output; }  static List<int[]> getAllBinaryCombinations(int numElements); static int[] getRandPermutation(int size); static int[] getRandPermutation(int size, Random rand); static List<List<T>> crossProduct(List<List<T>> input); static void main(String[] args);  }
@Test public void testGetAllBinaryCombinations() { for (int numElements : new int[] {3, 4}) { List<int[]> combs = Permutations.getAllBinaryCombinations(numElements); assertEquals((int) (Math.pow(2, numElements)), combs.size()); Counter<Integer> numOnes = new Counter<>(); for (int[] a : combs) { for (int i : a) { assertEquals(true, i == 0 || i == 1); } int sum = ArrayUtilities.sum(a); numOnes.incrementCount(sum); } for (int i = 0; i < numElements; i++) { assertEquals((double) MathUtilities.binomialCoeffs(numElements, i), numOnes.getCount(i), 0.0); } } }