Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: list of four digit lock combinations without repeated digits

by Lotus1 (Priest)
on Jun 22, 2018 at 14:11 UTC ( #1217197=note: print w/replies, xml ) Need Help??


in reply to Re: list of four digit lock combinations without repeated digits
in thread list of four digit lock combinations without repeated digits

I converted this pseudo code to Perl and found that it produces the 5040 permutations.

use warnings; use strict; my @digits_used; #$digits_used = [false x 10]; find_digits(1, 0); #// get the ball rolling sub find_digits { my ($depth, $total) = @_; for my $digit (0..9) { if( not $digits_used[$digit]) { $digits_used[$digit] = 1; if ($depth < 4) { find_digits($depth+1, ($total+$digit)*10); #// recur +se } else { print $total+$digit,"\n"; } $digits_used[$digit] = 0; } #// if used } #// for digits } #// end sub __END__ c:\usr\scripts>sundial.pl | find /c /v "" 5040

I've always been fascinated with recursive code but there are times when it isn't a good fit for a problem. Those times are any time there is a simpler solution. To produce the 5040 permutations without repeats you only need to eliminate duplicate digits from 0..9999. This can be done with a simple regex and sprintf on a Perl one liner. This looks like a good start for a competition for code obfuscation. Where did you find this code?

Replies are listed 'Best First'.
Re^3: list of four digit lock combinations without repeated digits
by karlgoethebier (Monsignor) on Jun 22, 2018 at 17:21 UTC
    "... pseudo code..."

    Very optimistic. See also.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2019-06-17 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (78 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!