dpaukov / combinatoricslib

Combinatorial Objects Generators for Java 7+.
GNU Lesser General Public License v3.0
88 stars 15 forks source link

permutation without repetition with selected object r from n things #20

Closed ramAdam closed 7 years ago

ramAdam commented 7 years ago

is it possible to do permutation without repetition with selected objects r ? P(n , r) = n! / (n - r)!

where n is the number of things to choose from, and we choose r of them Is there any API doc available for the library?

dpaukov commented 7 years ago

If you need to generate the permutations you can look at the documentation here. More details can be found in the permutation iterator class. Method next() produces next permutation every time it is called during iterating over all permutations.

ramAdam commented 7 years ago

@dpaukov thanks

ramAdam commented 7 years ago

anyone looking for the answer to the question above may find the following answer useful

/*permutation without repetition*/

   ICombinatoricsVector<String> originalVector = Factory.createVector(new String[] { "apple", "orange", "banana" });
   Generator<String> gen = Factory.createPermutationWithRepetitionGenerator(originalVector, 2);

  for (ICombinatoricsVector<String> perm : gen){
      if( !perm.hasDuplicates())      //filters all the ones without duplicates
           System.out.println( perm );
   }
dpaukov commented 7 years ago

@ramAdam your example above will return nothing, as you try to generate 3-permutations of the 2 elements set of ("apple", "orange"). All the permutations will have duplicates in the example. Permutations' length shouldn't be greater than the cardinality of the set.

ramAdam commented 7 years ago

@dpaukov you are correct, i copy pasted the code from docs, forgot to edit the elements of the set . I edited the post again, could you please check now. Thanks