Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ofPermutations(List<V> list) #130

Open
numeralnathan opened this issue Dec 2, 2016 · 4 comments
Open

ofPermutations(List<V> list) #130

numeralnathan opened this issue Dec 2, 2016 · 4 comments

Comments

@numeralnathan
Copy link

StreamEx has a method called ofPermutations(int length). This creates a StreamEx<int[]>.

I would like a method ofPermutations(List list) which returns StreamEx<List>. The returned stream has all of the permutations of the given list. However, the stream has distinct Lists (i.e. list1 != list2 for any given pair of Lists).

One could implement this with a mapping function. However, this results in allocating the int[] and the List.

@amaembo
Copy link
Owner

amaembo commented Dec 3, 2016

That was planned. The difficulty is that people would assume that if list contains equal elements, then permutations should not be duplicated. E.g. ofPermutations(asList(1,1,2)) would yield only 1,1,2, 1,2,1 and 2,1,1. That is, three elements instead of six. Also ofPermutations(asList(1,1,1)) should yield only one permutation. It's possible to implement but somewhat more difficult, especially if we want to support nice parallelization.

@numeralnathan
Copy link
Author

I think if you document that duplicate Lists will be generated if duplicate elements are in the source list. Once could always do the following to eliminate duplicates...

ofPermutations(asList(1,1,2)).
   distinct()

@amaembo
Copy link
Owner

amaembo commented Dec 4, 2016

I guess, it would be much less efficient and take much more memory, compared to possible optimized implementation. Here some of possible solutions are listed.

@numeralnathan
Copy link
Author

numeralnathan commented Dec 5, 2016

Here is a pseudo algorithm which can calculate the permutation given the lexicographical index. In other words, this can be used to split the Spliterator in O(1) time since all it requires is assigning an index range to each Spliterator. Here is some simple Python code. The Python code is a little slow since it computes the factorial each iteration of the loop instead of adjusting the factorial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants