It's a perfectly rational choice, you just have to know it.
However, the weird thing with this particular conversion is that I originally converted
for j in range(a[t - p] + 1, k): to
for my $j ( $a[ $t - $p ] + 1 .. $k-1 ) {
but the code didn't work and I had to switch it to
for my $j ( $a[ $t - $p ] + 1 .. $k ) { to make it work.
Here's my conversion:
{ my( $n, $k, @seq, @a ); sub db { my( $t, $p ) = @_; if( $t > $n ) { push @seq, @a[ 1 .. $p ] if ( $n % $p ) == 0; } else { $a[ $t ] = $a[ $t - $p ]; db( $t+1, $p ); for my $j ( $a[ $t - $p ] + 1 .. $k ) { ## Ought to be $k- +1, but that doesn't work! $a[ $t ] = $j; db( $t+1, $t ); } } } sub deBruijn { # de Bruijn sequence for alphabet k # and subsequences of length n. undef @seq; undef @a; ( $k, $n ) = @_; my @alphabet = split( '', $k ); $k = $#alphabet; @a = (0) x ( $k * $n ); db( 1, 1 ); return join( '', @alphabet[ @seq ] ); } }
I omitted the feature where the first parameter is inspected for being an integer and alphabet 0 .. k-1 is generated internally because it make using a none zero-based, or non-sequential integers alphabet impossible. Ie. supply k as '3579' and instead of being taken as an alphabet of 4 characters, it gets converted to a 3k letter alphabet and blows your memory.
Besides, it's far simpler to create an sequence externally and pass it in.
And I've made no attempt to optimise it, because now I have a working Perl version for comparison, I'll code a fast version in either Julia or C.
In reply to Re^4: [OT] Python to Perl.
by BrowserUk
in thread [OT] Python to Perl.
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |