Permutator test : Algorithms « Collections Data Structure « Java






Permutator test

        


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

/**
 *
 * @author  Andreou Dimitris, email: jim.andreou (at) gmail (dot) com 
 */
public class Permutator {
    private Permutator() { }
    
    public static <T> Iterable<List<T>> permutations(final List<T> list) {
        return new Iterable<List<T>>() {
            public Iterator<List<T>> iterator() {
                return new Iterator<List<T>>() {
                    private int current = 0;
                    private final long length = factorial(list.size());
                    
                    public List<T> next() {
                        if (!hasNext()) {
                            throw new NoSuchElementException();
                        }
                        List<T> permutation = new ArrayList<T>(list);
                        int k = current;
                        for (int j = 2; j <= list.size(); j++) {
                            k /= j - 1;
                            Collections.swap(permutation, (k % j), j - 1);
                        }
                        current++;
                        return permutation;
                    }
                    
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                    
                    public boolean hasNext() {
                        return current < length;
                    }
                };
            }
        };
    }
    
    private static long factorial(int k) {
        long factorial = 1L;
        for (int i = 2; i <= k; i++) {
            factorial *= i;
        }
        return factorial;
    }
}

// * @author  Andreou Dimitris, email: jim.andreou (at) gmail (dot) com 
/*
class PermutatorTest extends TestCase {
    
    public PermutatorTest(String testName) {
        super(testName);
    }

    public void testPermutations() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
        Set<List<Integer>> allLists = new HashSet<List<Integer>>();
        for (List<Integer> permutation : Permutator.permutations(list)) {
            allLists.add(permutation);
        }
        assertEquals(allLists.size(), 1 * 2 * 3 * 4 * 5 * 6);
    }

}
*/

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.AnagramsAnagrams
2.Hanoi puzzleHanoi puzzle
3.FibonacciFibonacci
4.Sieve Sieve
5.Find connections using a depth-first searchFind connections using a depth-first search
6.Find connections using hill climbing.
7.Find optimal solution using least-cost
8.Find the lost keysFind the lost keys
9.Compute the area of a triangle using Heron's FormulaCompute the area of a triangle using Heron's Formula
10.Compute prime numbers
11.Print a table of fahrenheit and celsius temperatures 1
12.Print a table of fahrenheit and celsius temperatures 2
13.Print a table of Fahrenheit and Celsius temperatures 3Print a table of Fahrenheit and Celsius temperatures 3
14.Soundex - the Soundex Algorithm, as described by KnuthSoundex - the Soundex Algorithm, as described by Knuth
15.A programmable Finite State Machine implementation.
16.An extendable Graph datastructure.
17.Utilities for flop (floating-point operation) counting.
18.LU Decomposition
19.Reverse Polish Notation
20.implements the LZF lossless data compression algorithm
21.Linear Interpolation
22.Utility class for generating the k-subsets of the numbers 0 to n
23.VersionVersion