I'm aware that this may be someone's homework, but that may be a good reason to try to make it interesting for all parties. I made life-long friends in combo, what Dr. Jay Goldman at UMN called "fancy counting." He was proud that he had never compiled a computer program. I would have gotten twenty bucks if I could have gotten him to compile:

./1.permute.pl : permutations for 851: 8,5,1 8,1,5 5,8,1 5,1,8 1,8,5 1,5,8 $ cat 1.permute.pl #!/usr/bin/perl -w use 5.011; use Algorithm::Combinatorics qw/permutations/; die "Usage : $0 number" unless scalar @ARGV; my @digits = split //, $ARGV[0]; my $iter = permutations( \@digits ); print "$0 : permutations for $ARGV[0]:\n"; while ( my $p = $iter->next ) { print " " . join( ",", @$p ) . "\n"; } $

I think reasonable people could disagree as to what the "next" permutation is. Is the underlying set the natural numbers? Left to right is big to small? I thought I would take a swing at the next method:

#!/usr/bin/perl -w
use 5.011;
# https://metacpan.org/pod/Algorithm::Combinatorics
# first method implemented by bliako
use Algorithm::Combinatorics qw/permutations/;

die "Usage : $0 number" unless scalar @ARGV;

my @digits = split //, $ARGV[0];

my $iter = permutations( \@digits );
print "$0 : permutations for $ARGV[0]:\n";
while ( my $p = $iter->next ) {
  print "  " . join( ",", @$p ) . "\n";
}

# try next method with hungarian symbols
#
use utf8;
my @Erdős = permutations(\@digits);
say "airdish is";
say "@Erdős";
use Data::Dump;
dd \@Erdős;
my @descending = sort { $b <=> $a } @Erdős;
say "descending is";
dd \@descending;
$ ./2.permute.pl 372 ./2.permute.pl : permutations for 372: 3,7,2 3,2,7 7,3,2 7,2,3 2,3,7 2,7,3 airdish is ARRAY(0x5621ec17ad00) ARRAY(0x5621ec17ae80) ARRAY(0x5621ec17aec8) ARRA +Y(0x5621ec23e770) ARRAY(0x5621ec2d4d80) ARRAY(0x5621ec2d5158) [[3, 7, 2], [3, 2, 7], [7, 3, 2], [7, 2, 3], [2, 3, 7], [2, 7, 3]] descending is [[2, 7, 3], [2, 3, 7], [7, 2, 3], [7, 3, 2], [3, 2, 7], [3, 7, 2]] $

Ordering these could turn into a boondoggle pretty quickly. Update: the use of word "method" in this post was technically misleading/misguided, as no objects are created. The word "function" is correct.


In reply to Re^2: Next permutation by Aldebaran
in thread Next permutation by aartist

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.