http://www.perlmonks.org?node_id=1217140


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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^2: list of four digit lock combinations without repeated digits
by Lotus1 (Vicar) on Jun 22, 2018 at 14:11 UTC

    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?

      "... 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