Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by amar712 (Initiate)
on Apr 25, 2016 at 22:30 UTC ( [id://1161491]=perlquestion: print w/replies, xml ) Need Help??

amar712 has asked for the wisdom of the Perl Monks concerning the following question:

How do I write a code to generate 9-unique digit number sequence in descending order. Eg. 987654321, 987654312, ....
  • Comment on Perl program to generate a descending order of 9-unique digit numbers

Replies are listed 'Best First'.
Re: Perl program to generate a descending order of 9-unique digit numbers
by BrowserUk (Patriarch) on Apr 25, 2016 at 22:41 UTC

    use Algorithm::Combinatorics:

    use Algorithm::Combinatorics qw[ permutations ]; my $iter = permutations( [ reverse 0 .. 9 ] ); print join '', @$_ while defined( $_ = $iter->next ); 9876543210 9876543201 9876543120 9876543102 9876543021 9876543012 9876542310 9876542301 9876542130 9876542103 9876542031 9876542013 9876541320 ...

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      If 0 is included in digits, one can use variations.

      $ perl -MAlgorithm::Combinatorics=:all -le 'print @$_ for variations [reverse 0..9], 9;'
Re: Perl program to generate a descending order of 9-unique digit numbers
by Anonymous Monk on Apr 26, 2016 at 04:04 UTC

    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

      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
      $_ = 987654321; # oops, 9 unique digits
Re: Perl program to generate a descending order of 9-unique digit numbers
by jdporter (Paladin) on Apr 26, 2016 at 13:30 UTC

    It's instructive to see how this might be coded. Recursion works well here.

    use strict; use warnings; # call as: # full_peal( \@remaining_bells, built-up-path-in-tree ) sub full_peal { my $bells_ar = shift; # @_ now contains the path so far if ( @$bells_ar == 0 ) # we've reached a "leaf" { local( $\, $, ) = ( "\n", " " ); print @_; # or whatever you want to do with this permutation return; } # pull out each item in turn for my $i ( 0 .. $#{$bells_ar} ) { # make a local destructable copy my @bells = @{$bells_ar}; # pull out the item my $bell = splice @bells, $i, 1; # recurse, with this item added to the branch full_peal( \@bells, @_, $bell ); } } my @bells = reverse( 1..3 ); # descending order # begin, with an empty tree full_peal( \@bells );

    Any questions?

    I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.
Re: Perl program to generate a descending order of 9-unique digit numbers
by QuillMeantTen (Friar) on Apr 26, 2016 at 10:20 UTC

    Looks like pm IS a code writing service after all...

      I can never remember whether this kind of thing requires permutations or combinations; and the quickest way for me to be sure is to write the three lines of code it takes to check.

      Once those three lines are written, don't you think it would be bloody mean to withhold them, just so that those who's only contribution to PM is to post "This isn't a code writing service.", are proved correct?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Who said anything about witholding them?
        Give a man a fish and all that, instead of doing a quick brain dump of the solved problem one can also explain the reasoning behind one's solution.

        Here you could have explained why you can never remember between permutations, combinations, then maybe give a bit of math background regarding the number of permutations/combinations for n numbers and then explain how you came up with that answer

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-16 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found