Code-Golf: Permutations
Python, 52
Input is a set. Output is a list of lists.
f=lambda a:[p+[x]for x in a for p in f(a-{x})]or[[]]
This is shorter than the answer that does all the work with a builtin.
Python - 76 chars
Longer than gnibbler's, but implements things from scratch.
p=lambda x:x and[[a]+b for a in x for b in p([c for c in x if c!=a])]or[[]]
J, 11 characters
(i.@!@#A.[)
Usage:
(i.@!@#A.[) 1 3 5
1 3 5
1 5 3
3 1 5
3 5 1
5 1 3
5 3 1
Explanation:
i.@!@#
uses three verbs to return a list from 0 to (!n)-1 where n is the number of items in the given list.
[
returns the list itself. In the example shown that gives 0 1 2 3 4 5 A. 1 3 5
.
A.
returns one possible permutation of the second list for each item in the first list (kind of - the proper explanation is given here).