Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This code works out the variations, then runs the permutations on each unique set of 3.</p> <p>i.e. for "A", "B", "C", "D" the possibilities are [[A, B, C], [A, B, D], [A, C, D], [B, C, D]]. We then calculate the permutations on each threesome (or n-some) and append the possibilities to a list.</p> <p><strong>PermutationsOfN.processSubsets( List set, int k )</strong> returns: [[A, B, C], [A, B, D], [A, C, D], [B, C, D]]</p> <p>Taking it a bit further <strong>PermutationsOfN.permutations( List list, int size )</strong> returns:<br> [[A, B, C], [A, C, B], [C, A, B], [C, B, A], [B, C, A], [B, A, C], [A, B, D], [A, D, B], [D, A, B], [D, B, A], [B, D, A], [B, A, D], [A, C, D], [A, D, C], [D, A, C], [D, C, A], [C, D, A], [C, A, D], [B, C, D], [B, D, C], [D, B, C], [D, C, B], [C, D, B], [C, B, D]]</p> <pre><code>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&lt;T&gt; { public static void main( String[] args ) { List&lt;String&gt; f = Lists.newArrayList( "A", "B", "C", "D" ); PermutationsOfN&lt;String&gt; g = new PermutationsOfN&lt;String&gt;(); 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", g.processSubsets( f, 2 ) ) ); System.out.println( String.format( "n=2 permutations %s", g.permutations( f, 2 ) ) ); System.out.println( String.format( "n=3 subsets %s", g.processSubsets( f, 3 ) ) ); System.out.println( String.format( "n=3 permutations %s", g.permutations( f, 3 ) ) ); System.out.println( String.format( "n=4 subsets %s", g.processSubsets( f, 4 ) ) ); System.out.println( String.format( "n=4 permutations %s", g.permutations( f, 4 ) ) ); System.out.println( String.format( "n=5 subsets %s", g.processSubsets( f, 5 ) ) ); System.out.println( String.format( "n=5 permutations %s", g.permutations( f, 5 ) ) ); } public List&lt;List&lt;T&gt;&gt; processSubsets( List&lt;T&gt; set, int k ) { if ( k &gt; set.size() ) { k = set.size(); } List&lt;List&lt;T&gt;&gt; result = Lists.newArrayList(); List&lt;T&gt; subset = Lists.newArrayListWithCapacity( k ); for ( int i = 0; i &lt; k; i++ ) { subset.add( null ); } return processLargerSubsets( result, set, subset, 0, 0 ); } private List&lt;List&lt;T&gt;&gt; processLargerSubsets( List&lt;List&lt;T&gt;&gt; result, List&lt;T&gt; set, List&lt;T&gt; subset, int subsetSize, int nextIndex ) { if ( subsetSize == subset.size() ) { result.add( ImmutableList.copyOf( subset ) ); } else { for ( int j = nextIndex; j &lt; set.size(); j++ ) { subset.set( subsetSize, set.get( j ) ); processLargerSubsets( result, set, subset, subsetSize + 1, j + 1 ); } } return result; } public Collection&lt;List&lt;T&gt;&gt; permutations( List&lt;T&gt; list, int size ) { Collection&lt;List&lt;T&gt;&gt; all = Lists.newArrayList(); if ( list.size() &lt; size ) { size = list.size(); } if ( list.size() == size ) { all.addAll( Collections2.permutations( list ) ); } else { for ( List&lt;T&gt; p : processSubsets( list, size ) ) { all.addAll( Collections2.permutations( p ) ); } } return all; } } </code></pre> <p>A special mention goes to <a href="https://stackoverflow.com/users/183406/meriton">meriton</a> whose answer <a href="https://stackoverflow.com/questions/4504974/how-to-iteratively-generate-k-elements-subsets-from-a-set-of-size-n-in-java">here</a> helped me work it out.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload