in reply to Re: list of four digit lock combinations without repeated digits
in thread list of four digit lock combinations without repeated digits
> 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
real 0m0.858s
4 is only ~2MB but 5 is 70MB and the 6 character array is around 2000MB.
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
Using Algorithm::Combinatorics: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
real 0m0.858s
Be careful with the parameter because this can make a very big array.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
4 is only ~2MB but 5 is 70MB and the 6 character array is around 2000MB.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: list of four digit lock combinations without repeated digits
by BrowserUk (Patriarch) on Jun 21, 2018 at 04:34 UTC | |
by usemodperl (Beadle) on Jun 21, 2018 at 08:18 UTC | |
by BrowserUk (Patriarch) on Jun 21, 2018 at 10:32 UTC | |
by usemodperl (Beadle) on Jun 22, 2018 at 02:35 UTC | |
by BrowserUk (Patriarch) on Jun 22, 2018 at 06:12 UTC | |
|
In Section
Seekers of Perl Wisdom