Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This permutation generator generates only unique permutations, even when the list to be permuted contains duplicates. It can be used for huge lists since the list of permutations is not built in memory (and we don't have to keep any "context" info around). To use it, first sort the array, then each call to nextPermute() will permute the array in place, returning false when all permutations have been done (which also means that the array was just in reverse sorted order and has been left that way).

Note that the permutations get generated in "sorted order". For example, if all of the items are gt " ", then the output of the sample usage would already be sorted.

The call to reverse was originally a call to sort. When I noticed this inefficiency, I compared the improved version against Algorith::Permute and all of the permutation finders that it included in its benchmark. My (improved) code was faster than all of them except Algorith::Permute's [ which are written in XS, so being only about 1/2 as fast was quite acceptable to me (: ]. Note also that the version below handles duplicates while none of the other fine algorithms did.

Update: Did s/nextpermute/nextPermute/.

Update: Now available as part of Algorithm::Loops.

Update: Replaced 'while' with 'until' as the comparisons where "backward" (implied negations).

sub nextPermute(\@) { my( $vals )= @_; my $last= $#{$vals}; return "" if $last < 1; # Find last item not in reverse-sorted order: my $i= $last-1; $i-- until $i < 0 || $vals->[$i] lt $vals->[$i+1]; # If complete reverse sort, we are done! return "" if -1 == $i; # Re-sort the reversely-sorted tail of the list: @{$vals}[$i+1..$last]= reverse @{$vals}[$i+1..$last] if $vals->[$i+1] gt $vals->[$last]; # Find next item that will make us "greater": my $j= $i+1; $j++ until $vals->[$i] lt $vals->[$j]; # Swap: @{$vals}[$i,$j]= @{$vals}[$j,$i]; return 1; } #Code to make the sample use below friendly: if( 0 == @ARGV ) { die "Usage: $0 word\n", " or: $0 t o k e n s\n", "Prints all unique permutations of the letters or words given. +\n"; } elsif( 1 == @ARGV ) { @ARGV= $ARGV[0] =~ /(.)/gs; $"= ""; } #Sample use: @ARGV= sort @ARGV; do { print "@ARGV\n"; } while( nextPermute(@ARGV) );

In reply to Permuting with duplicates and no memory by tye

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-03-29 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found