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

In reply to Re^4: [OT] Python to Perl. by BrowserUk
in thread [OT] Python to Perl. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.