Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: list of four digit lock combinations without repeated digits

by BrowserUk (Pope)
on Jun 20, 2018 at 19:24 UTC ( #1217044=note: print w/replies, xml ) Need Help??


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

Algorithm::Combinatorics is good for this (though it sometimes takes trial and error to work out which of it algorithms you need), and it generates these kinds of patterns very efficiently:

#! perl -slw use strict; use Algorithm::Combinatorics qw[:all]; my $iter = combinations( [0..9], 4 ); print "@$_" while $_ = $iter->next; __END__ C:\test>4of10Combinations.pl | wc -l 210

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". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

Replies are listed 'Best First'.
Re^2: list of four digit lock combinations without repeated digits
by usemodperl (Beadle) on Jun 21, 2018 at 01:23 UTC
    > it generates these kinds of patterns very efficiently

    I tried Algorithm::Combinatorics for a similar task. Both of these examples generate an array of strings n characters in length of all combinations of letters (e.g. 4 = 'aaaa'..'zzzz'). Am I doing something stupid with the module, or perl?

    Perl string iteration:
    real 0m0.129s
    time perl -wle ' $n = shift; die "need a number" unless $n and $n =~ /^\d+$/; $a = "a" x $n; $c = 0; $e = 26**$n; while () { push @x, $a; $a++ and $c++; last if $c == $e } print scalar @x' 4
    Using Algorithm::Combinatorics:
    real 0m0.858s
    time perl -MAlgorithm::Combinatorics=:all -wle ' $n = shift; die "need a number" unless $n and $n =~ /^\d+$/; @_ = ("a".."z"); $i = variations_with_repetition(\@_,$n); while ($c = $i->next){ for (@$c) { $x .= $_ } push @x, $x; undef $x } print scalar @x' 4
    Be careful with the parameter because this can make a very big array.
    4 is only ~2MB but 5 is 70MB and the 6 character array is around 2000MB.
      Am I doing something stupid with the module, or perl?
      1. This: for(@$c){ $x .= $_ } is a really slow way to build a string from an array.
      2. Package variables are slower than lexicals.
      3. Postfix loops generally run more quickly than those with scoped bodies.

      Try this and see how it compares on your system:

      perl -MAlgorithm::Combinatorics=:all -wle'my $i=variations_with_repeti +tion(["a".."z"],$ARGV[0]); my @x; push @x, qq[@$_] while $_=$i->next; + print scalar @x' 4

      In general, those algorithms that require more selection than variations_with_repetition() -- almost any of the other algorithms -- is where A;:C shines over a pure perl implementation.


      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". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
        Very interesting info, thank you. I often wonder about such efficiency when writing perl but it doesn't usually matter too much. I do write postfix loops because they're pretty and one-line friendly (I ❤ print$_ for@_) but not when they're nested because I tend to forget about them. If they're really much faster I could use comments to remind myself. Anyway your version is slightly faster (~0.750) but the data has whitespace and when using map to remove that it gets even slower than mine. What's going on?

        time perl -MAlgorithm::Combinatorics=:all -wle' my $i=variations_with_repetition(["a".."z"],$ARGV[0]); my @x; push @x, map {s/ //gr} qq[@$_] while $_=$i->next; print scalar @x' 4
        real 0m1.050s

        Profile:
        perl -d:NYTProf -MAlgorithm::Combinatorics=:all -wle' my $i=variations_with_repetition(["a".."z"],$ARGV[0]); my @x; push @x, map {s/ //gr} qq[@$_] while $_=$i->next; print scalar @x' 4; nytprofhtml --open

        STOP REINVENTING WHEELS, START BUILDING SPACE ROCKETS!CPAN 🐫
Re^2: list of four digit lock combinations without repeated digits
by Lotus1 (Priest) on Jun 20, 2018 at 20:10 UTC

    Wow! That is great. Thanks

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2019-06-17 21:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (80 votes). Check out past polls.

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