Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: [OT] Python to Perl.

by Marshall (Canon)
on Jun 15, 2018 at 16:07 UTC ( [id://1216723]=note: print w/replies, xml ) Need Help??


in reply to [OT] Python to Perl.

No. (as you have already found out)

I don't know how to do this seemingly simple task in Python without an executable loop.

If you figure out a way to do this, please let me know.

Replies are listed 'Best First'.
Re^2: [OT] Python to Perl.
by BrowserUk (Patriarch) on Jun 15, 2018 at 16:10 UTC

    Actually yes. (Or whatever differences tehre are do not matter to the code I am converting as it now works.)

    It was another part of the code that was at fault; a range( 1 .. k ) that I had converted to 1 .. $k instead of 1..$k-1.


    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

      Ah, this makes sense at some wacky level.

      Consider this array (language doesn't matter here, so this is pseudo-code):

      array = ('a', 'b', 'c', 'd', 'e')

      Some languages use a convention where begin(array) is equivalent to array[0], and end(array) is equivalent to array[5], or one past the last element. A loop in such a world might be written like this:

      for (x = 0; x != end(array); ++x) do_something(array, x);

      The notation used for such a concept is sometimes this:

      [0,5)

      ...meaning (0 <= x < 5). This concept and notation is common in C++. And is used by Python's range() function.

      Python's range method has the following semantics:

      range(stop) # 0 .. stop-1 range(start,stop) # start .. stop-1 range(start,stop,step) # start .. stop-1 by step: for(ix=0; ix != sto +p; ix += step) {...}

      In each case start is inclusive, and stop is exclusive. [start,stop) in mathematical notation, or this: (start <= x < stop).


      Dave

        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
        ... this makes sense at some wacky level.

        For me, the semantic sense is that in a 0-based array, the length of the array can be used to represent its highest index. A D example:

        import std.stdio; void main () { auto ra = [ 'a', 'b', 'c', 'd', 'e', ]; writefln("length of ra is %s", ra.length); writeln(); write("A: "); foreach (i; 0 .. ra.length) { writef("'%s' @ index %d ", ra[i], i); } writeln(); write("B: "); foreach (i, e; ra[ 0 .. $ ]) { writef("ra[%d] == '%s' ", i, e); } writeln(); write("C: "); foreach (i, e; ra) { writef("ra[%d] == '%s' ", i, e); } }
        Output:
        C:\@Work\DMD2\posts\davido> python_to_d_1 length of ra is 5 A: 'a' @ index 0 'b' @ index 1 'c' @ index 2 'd' @ index 3 'e' @ +index 4 B: ra[0] == 'a' ra[1] == 'b' ra[2] == 'c' ra[3] == 'd' ra[4] == ' +e' C: ra[0] == 'a' ra[1] == 'b' ra[2] == 'c' ra[3] == 'd' ra[4] == ' +e'
        (The expression  ra[ 0 .. $ ] is shorthand for  ra[ 0 .. ra.length ])


        Give a man a fish:  <%-{-{-{-<

        IMHO the main advantage of these kind of ranges is that you can easily partition ranges in smaller ones:

        [0..9[ = [0..3[ + [3..6[ + [6..9[ (pseudocode!)

        This "scales" well (in a mathematical sense) without needing to add or subtract at the edges, hence less error prone.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-28 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found