Closed ppareit closed 8 years ago
This feature is already supported by the library (from the version 2.1). Class PermutationGenerator
can detect if an initial vector has duplicates and treat them accordingly by using the DuplicatedPermutationIterator
. You can look at the unit test here PermutationsTest.test_anagram_generation()
or try to run the following code:
ICombinatoricsVector<String> originalVector = Factory.createVector("aabc".split(""));
// Create a permutation generator. The generator can detect that the vector has duplicates Generator<String> gen = Factory.createPermutationGenerator(originalVector);
// Print 12 anagrams: aabc, aacb, abac, abca, acab, acba, baac, baca, bcaa, caab, caba, cbaa
for (ICombinatoricsVector<String> perm : gen)
System.out.println(perm);
You will get all 12 anagrams as you describe in this pull request
CombinatoricsVector=([a, a, b, c], size=4)
CombinatoricsVector=([a, a, c, b], size=4)
CombinatoricsVector=([a, b, a, c], size=4)
CombinatoricsVector=([a, b, c, a], size=4)
CombinatoricsVector=([a, c, a, b], size=4)
CombinatoricsVector=([a, c, b, a], size=4)
CombinatoricsVector=([b, a, a, c], size=4)
CombinatoricsVector=([b, a, c, a], size=4)
CombinatoricsVector=([b, c, a, a], size=4)
CombinatoricsVector=([c, a, a, b], size=4)
CombinatoricsVector=([c, a, b, a], size=4)
CombinatoricsVector=([c, b, a, a], size=4)
Yes, I see. Thanks for the example, I'll study the test cases more closely.
In my client code it helped to think about it using a separate class. Also the getGeneratedCount() function had to be implemented anyway. This can be lifted from the code and added in the PermutationGenerator class.
The heavy work is done by the DuplicatedPermutationIterator. This class makes client code a lot simpler.
It would be useful to me (and probably others) if this class was added to the library. Also a 2.3 release would be nice. I'm working on a project that uses combinatoricslib, no release yet, but soon.