Created
June 4, 2011 15:37
-
-
Save thomie/1007989 to your computer and use it in GitHub Desktop.
Unique permutations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate all unique permutations of an array | |
# with possibly duplicate elements (multiset). | |
# unique_permutations(('A', 'B', 'B')) -> ABB BAB BBA | |
sub unique_permutations | |
{ | |
my @results; | |
my (@list) = @_; | |
if ($#list == -1) { | |
return [()]; | |
} | |
else { | |
# Create a hash with the unique elements of the list as keys and the index | |
# of these elements in the list as values. | |
my %index_hash; | |
@index_hash{@list} = (0..$#list); | |
# Loop over the unique elements of the list. | |
my ($unique_element, $index); | |
while (($unique_element, $index) = each(%index_hash)) { | |
# Create a sublist of the list excluding the unique element. | |
my @sublist = @list; | |
splice(@sublist, $index, 1); | |
# Loop over all the permutations of the sublist (recursively). | |
for my $subpermutations (unique_permutations(@sublist)) { | |
# Append the unique element to this subpermutation and add the result | |
# to the final list of results. | |
push @results, [(@$subpermutations, $unique_element)]; | |
} | |
} | |
return @results; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unique_permutations(iterable): | |
# Generate all unique permutations of an iterable | |
# with possibly duplicate elements (multiset). | |
# unique_permutations('ABB') -> ABB BAB BBA | |
if not iterable: | |
yield [] | |
else: | |
xs = list(iterable) | |
for x in set(xs): | |
i = xs.index(x) | |
for result in unique_permutations(xs[:i] + xs[i + 1:]): | |
yield result + [x] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspiration: http://mail.python.org/pipermail/python-list/2009-January/1189787.html