Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: [OT] Python to Perl.

by BrowserUk (Patriarch)
on Jun 15, 2018 at 18:47 UTC ( [id://1216742]=note: print w/replies, xml ) Need Help??


in reply to Re^3: [OT] Python to Perl.
in thread [OT] Python to Perl.

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.


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-26 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found