dpaukov / combinatoricslib3

Combinatorial objects stream generators for Java.
Apache License 2.0
178 stars 23 forks source link

k-permutations #6

Closed sdemarch closed 5 years ago

sdemarch commented 5 years ago

Hi,

Thanks for your excellent work, this is truly handful AND elegant, which is remarkable. I was wondering if you are going to add k-permutations in your library, would definitely make it 100% awesome.

Regards

dpaukov commented 5 years ago

Hello Stefano,

Thank you for the feedback.

As for k-permutations. There is no a special generator for this type of permutation in the library, but you can always generate them using the existing Combination and Permutation generators together. For example, 2-Permutations without repetitions of the lists (1, 2, 3) can be generated using the following code:

        Generator.combination(1, 2, 3)
            .simple(2)
            .stream()
            .forEach(combination -> Generator.permutation(combination)
                .simple()
                .forEach(System.out::println));

it will print the following six 2-permutations of (1, 2, 3):

   [1, 2]
   [2, 1]
   [1, 3]
   [3, 1]
   [2, 3]
   [3, 2]

Similarly, you can get 2-Permutations with repetitions of the lists (1, 2, 3):

        Generator.combination(1, 2, 3)
            .multi(2)
            .stream()
            .forEach(combination -> Generator.permutation(combination)
                .simple()
                .forEach(System.out::println));

it will print the following nine 2-permutations with repetitions of (1, 2, 3):

   [1, 1]
   [1, 2]
   [2, 1]
   [1, 3]
   [3, 1]
   [2, 2]
   [2, 3]
   [3, 2]
   [3, 3]

I have added these examples here. Let me know if you still think this is not what you need and a special generator should be added to the library.

sdemarch commented 5 years ago

Hey, thank you for your answer!

This is indeed what I was doing, but in two separate steps. I didn't think about the full power of Streams, and as long as this is possible I think it would be enough.

Thumbs up :)