Java – guava set: limit the size of the permutation
Use guava 12 collections2 Permutations(), I wonder if I can limit the size of the permutation?
More precisely, I want to get a K-size permutation list in a list of n elements, rather than a list of all n-size permutations
At present, if I pass a list of 4 fruits, permutations () will return 24 4-size permutations, although I am only interested in retrieving 4 unique size 3 permutations
Said I had a list of four fruits:
["Banana","Apple","Orange","Peach"]
If I'm only interested in arrangement 3, I'd like to return to the following:
["Banana","Orange"] ["Banana","Peach"] ["Banana","Peach"] ["Apple","Peach"]
Can anyone provide any hint of a solution? thank you!
Solution
This code calculates the variants and then runs the permutation on each unique 3 groups
That is, for "a", "B", "C", "d", the possibility is [[a, B, C], [a, D], C, [b, D] Then we calculate the permutation of each Trio (or n-some) and append the possibilities to the list
PermutationsOfN. Processsubsets (list set, int k) Returns: [[a, D]]
Further permutationsofn Permutations (list, int size) Returns: [[a, b], [C, a, a], [a, D, [D, D]]
import java.util.Collection; import java.util.List; import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; public class PermutationsOfN<T> { public static void main( String[] args ) { List<String> f = Lists.newArrayList( "A","B","C","D" ); PermutationsOfN<String> g = new PermutationsOfN<String>(); System.out.println( String.format( "n=1 subsets %s",g.processSubsets( f,1 ) ) ); System.out.println( String.format( "n=1 permutations %s",g.permutations( f,1 ) ) ); System.out.println( String.format( "n=2 subsets %s",2 ) ) ); System.out.println( String.format( "n=2 permutations %s",2 ) ) ); System.out.println( String.format( "n=3 subsets %s",3 ) ) ); System.out.println( String.format( "n=3 permutations %s",3 ) ) ); System.out.println( String.format( "n=4 subsets %s",4 ) ) ); System.out.println( String.format( "n=4 permutations %s",4 ) ) ); System.out.println( String.format( "n=5 subsets %s",5 ) ) ); System.out.println( String.format( "n=5 permutations %s",5 ) ) ); } public List<List<T>> processSubsets( List<T> set,int k ) { if ( k > set.size() ) { k = set.size(); } List<List<T>> result = Lists.newArrayList(); List<T> subset = Lists.newArrayListWithCapacity( k ); for ( int i = 0; i < k; i++ ) { subset.add( null ); } return processLargerSubsets( result,set,subset,0 ); } private List<List<T>> processLargerSubsets( List<List<T>> result,List<T> set,List<T> subset,int subsetSize,int nextIndex ) { if ( subsetSize == subset.size() ) { result.add( ImmutableList.copyOf( subset ) ); } else { for ( int j = nextIndex; j < set.size(); j++ ) { subset.set( subsetSize,set.get( j ) ); processLargerSubsets( result,subsetSize + 1,j + 1 ); } } return result; } public Collection<List<T>> permutations( List<T> list,int size ) { Collection<List<T>> all = Lists.newArrayList(); if ( list.size() < size ) { size = list.size(); } if ( list.size() == size ) { all.addAll( Collections2.permutations( list ) ); } else { for ( List<T> p : processSubsets( list,size ) ) { all.addAll( Collections2.permutations( p ) ); } } return all; } }
In particular, Meriton, the answer here, helped me solve the problem