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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |