in reply to Re: Perl program to generate a descending order of 9-unique digit numbers
in thread Perl program to generate a descending order of 9-unique digit numbers
Cleaned up and annotated. It's a perl version of https://en.m.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
#!/usr/bin/perl -l use strict; use warnings; $_ = 321; # for testing, change to 987654321 for final run 1 while print, s/.*\K # find the last (.) # digit such that (.*) # there is a later (latest) (.)(??{$1 < $3 and 'x'}) # digit less than it (.*) # and get rest # swap those two digits ( $1 & $3 ) # then reverse everything after the first swapped digit / $3 . reverse $2.$1.$4 /xe
|
|---|