Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Perl program to generate a descending order of 9-unique digit numbers

by Anonymous Monk
on Apr 26, 2016 at 04:04 UTC ( [id://1161516]=note: print w/replies, xml ) Need Help??


in reply to Perl program to generate a descending order of 9-unique digit numbers

This is, of course, doable with regex (what isn't?)

#!/usr/bin/perl use strict; use warnings; $\ = "\n"; $_ = 9876543210; my $n = 0; # to limit output $n++ < 12 or exit while print, s/.*\K (.) (?=(.)(??{$1 < $2 and 'x'}) (.*) (.)(??{$1 < $4 and 'x'}) (.*) / $4 . reverse $3.$1.$5 /xe
  • Comment on Re: Perl program to generate a descending order of 9-unique digit numbers
  • Download Code

Replies are listed 'Best First'.
Re^2: Perl program to generate a descending order of 9-unique digit numbers
by Anonymous Monk on Apr 26, 2016 at 15:39 UTC

    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
Re^2: Perl program to generate a descending order of 9-unique digit numbers
by Anonymous Monk on Apr 26, 2016 at 09:16 UTC
    $_ = 987654321; # oops, 9 unique digits

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1161516]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-24 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found