Saturday, May 24, 2014

Unimpressive/Unimpressive Code


    public static boolean permutation(List values, int index) {
        return permutation(values, values.size() - 1, index);
    }

    private static boolean permutation(List values, int n, int index) {
        if ((index == 0) || (n == 0)) {
            return (index == 0);
        }
        Collections.swap(values, n, n - (index % n));
        return permutation(values, n - 1, index / n);
    }

Give it a list, and an index and it will do the permutatation associated with that index. It runs out of integers at 12 items as 12! > Integer.MAX_VALUE

No comments: