in reply to Next permutation

# by bliako for https://perlmonks.org/?node_id=1230512 use strict; use warnings; 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"; }

bw, bliako

Replies are listed 'Best First'.
Re^2: Next permutation
by Aldebaran (Curate) on Feb 25, 2019 at 21:08 UTC

    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.

      I think reasonable people could disagree as to what the "next" permutation is.

      As seen by this survey of modules: Short survey of modules for combinations and permutations there is indeed disagreement. Lexicographic is the most common. Pari/GP's v2.6 changes include "permtonum/numtoperm now use the standard lexicographic numbering" so we might have some belief that that is a preferred ordering unless there are other requirements (e.g. Gray codes).

      There are lots of possible reasonable orderings, e.g. Jorg Arndt's Algorithms book section 6.1 (Permutations) where numerous methods are shown, or Ruskey and William's Cool-lex ordering from 2009 -- noted earlier in Knuth's Volume 4 Fascicle 3 (2005).

      I don't think most reasonable programmers would agree that "implementation defined and unlike every other implementation" is a good ordering. A number of modules on that list chose that so I guess there is disagreement. Some, like Math::Combinatorics, will give you a different ordering every run.