Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > FAQ 4.51 How do I permute N elements of a list?

Reply
Thread Tools

FAQ 4.51 How do I permute N elements of a list?

 
 
PerlFAQ Server
Guest
Posts: n/a
 
      02-07-2011
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.51: How do I permute N elements of a list?

Use the "List:ermutor" module on CPAN. If the list is actually an
array, try the "Algorithm:ermute" module (also on CPAN). It's written
in XS code and is very efficient:

use Algorithm:ermute;

my @array = 'a'..'d';
my $p_iterator = Algorithm:ermute->new ( \@array );

while (my @perm = $p_iterator->next) {
print "next permutation: (@perm)\n";
}

For even faster execution, you could do:

use Algorithm:ermute;

my @array = 'a'..'d';

Algorithm:ermute:ermute {
print "next permutation: (@array)\n";
} @array;

Here's a little program that generates all permutations of all the words
on each line of input. The algorithm embodied in the "permute()"
function is discussed in Volume 4 (still unpublished) of Knuth's *The
Art of Computer Programming* and will work on any list:

#!/usr/bin/perl -n
# Fischer-Krause ordered permutation generator

sub permute (&@) {
my $code = shift;
my @idx = 0..$#_;
while ( $code->(@_[@idx]) ) {
my $p = $#idx;
--$p while $idx[$p-1] > $idx[$p];
my $q = $p or return;
push @idx, reverse splice @idx, $p;
++$q while $idx[$p-1] > $idx[$q];
@idx[$p-1,$q]=@idx[$q,$p-1];
}
}

permute { print "@_\n" } split;

The "Algorithm::Loops" module also provides the "NextPermute" and
"NextPermuteNum" functions which efficiently find all unique
permutations of an array, even if it contains duplicate values,
modifying it in-place: if its elements are in reverse-sorted order then
the array is reversed, making it sorted, and it returns false; otherwise
the next permutation is returned.

"NextPermute" uses string order and "NextPermuteNum" numeric order, so
you can enumerate all the permutations of 0..9 like this:

use Algorithm::Loops qw(NextPermuteNum);

my @list= 0..9;
do { print "@list\n" } while NextPermuteNum @list;



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ 4.51 How do I permute N elements of a list? PerlFAQ Server Perl Misc 0 03-06-2011 05:00 AM
permute each element of a ragged array? Phlip Ruby 17 05-18-2009 04:34 PM
Randomly permute a list of integers Michael McGarry C Programming 6 08-11-2006 08:24 PM
function to permute a string Yuan Zhong C Programming 2 08-05-2004 12:32 AM



Advertisments