Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: list of four digit lock combinations without repeated digits

by johngg (Canon)
on Jun 20, 2018 at 22:06 UTC ( #1217078=note: print w/replies, xml ) Need Help??


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

A solution using glob to generate the 4-digit numbers, split, sort and join to get only ascending values then grep and a regex to sift out repeating digits. A hash is populated so that any duplicate values go into the same key/value pair and are therefore masked. Finally print the sorted values.

johngg@abouriou ~/perl/Monks $ perl -Mstrict -Mwarnings -E ' my $digs = q{0,1,2,3,4,5,6,7,8,9}; my $globStr = qq|{$digs}| x 4; my %combs = map { $_ => 1 } grep { ! m{(.).*\1} } map { join q{}, sort split m{} } glob $globStr; say for sort keys %combs;' | wc -l 210

I hope this is of interest.

Update: Can be shortened by acting directly on an anonymous hash but there's a warning unless you silence it (which, sadly, makes it not so short again).

johngg@abouriou ~/perl/Monks $ perl -Mstrict -Mwarnings -E ' my $digs = q{0,1,2,3,4,5,6,7,8,9}; my $globStr = qq|{$digs}| x 4; say for do { no warnings qw{ experimental::autoderef }; sort keys { map { $_ => 1 } grep { ! m{(.).*\1} } map { join q{}, sort split m{} } glob $globStr }; };' | wc -l 210

Cheers,

JohnGG

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

    I tried out your glob approach at number generation to understand it. Thanks for posting. I'm always trying to learn more about the usage of glob(). I found I could simplify your $globStr declaration like this:

    use warnings; use strict; my $digit_pattern = '{0,1,2,3,4,5,6,7,8,9}' x 4; print "$_\n" foreach glob $digit_pattern;

    When I saw your solution I realized I didn't need to test for the key existence in the hash. I could simply assign to the key. Thanks again.

    Edit:

    sort keys { map { $_ => 1 } grep { ! m{(.).*\1} } map { join q{}, sort split m{} } glob $globStr };
    I forgot to mention before an optimization I noticed: if there is a repeated digit in the number then there's no need to split and sort it. If you put the grep line after the map, join, sort, split line then it will filter out the repeats before the split(). The split, sort and assign to a hash approach is the exact same one that I took but mine were inside a foreach loop instead of in a map (loop).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2019-05-26 05:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (153 votes). Check out past polls.

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