The permutation may have more elements than slots. For example, the three possible permutation of 12 in three slots are: 111, 211, 121, 221, 112, 212, 122, and 222.
Please read more here http://code.google.com/p/combinatoricslib/
This is a code:
// create array of initial items
ArrayList<String> array = new ArrayList<String>();
array.add("one");
array.add("two");
// create combinatorics vector
CombinatoricsVector<String> corePermutation = new CombinatoricsVector<String>(array);
// create permutation with repetition generator, second parameter is a number of slots
Generator<String> permutationWithRepetitionGenerator = new PermutationWithRepetitionGenerator<String>(corePermutation, 3);
// create iterator
Iterator<CombinatoricsVector<String>> permutationWithRepetitionIterator = permutationWithRepetitionGenerator.createIterator();
// print the number of generated permutations
System.out.println("Number of permutationWithRepetition is: " + permutationWithRepetitionGenerator.getNumberOfGeneratedObjects());
permutationWithRepetitionIterator.first();
// go through the iterator
while (!permutationWithRepetitionIterator.isDone()) {
permutationWithRepetitionIterator.next();
CombinatoricsVector<String> permutationWithRepetition = permutationWithRepetitionIterator.getCurrentItem();
System.out.println(permutationWithRepetitionIterator);
}
And the result
PermutationWithRepetitionIterator=[#1, CombinatoricsVector=[[one, one, one]],size=3]]
PermutationWithRepetitionIterator=[#2, CombinatoricsVector=[[two, one, one]],size=3]]
PermutationWithRepetitionIterator=[#3, CombinatoricsVector=[[one, two, one]],size=3]]
PermutationWithRepetitionIterator=[#4, CombinatoricsVector=[[two, two, one]],size=3]]
PermutationWithRepetitionIterator=[#5, CombinatoricsVector=[[one, one, two]],size=3]]
PermutationWithRepetitionIterator=[#6, CombinatoricsVector=[[two, one, two]],size=3]]
PermutationWithRepetitionIterator=[#7, CombinatoricsVector=[[one, two, two]],size=3]]
PermutationWithRepetitionIterator=[#8, CombinatoricsVector=[[two, two, two]],size=3]]
No comments:
Post a Comment