in reply to Re^3: Merge 2 strings like a zip [benchmark]
in thread Merge 2 strings like a zip

BrowserUk:

When I saw your original version, I did basically the same thing (swap strings to make the shorter one last). The only problem is that it flips the order of the alternating characters--I expect the first argument to provide the first character, the second argument to provide the second, etc.

I tried a couple quick hacks on yours (and kcotts) version to make it work the way I wanted, but when I didn't get it quickly, I punted. My quick hacks caused the strings to truncate when I was munging with substr on the left because it seems that I can't count properly today--I got bit several times by fencepost errors. For example, one of my attempts was to use zipC and change the starting value of $n based on which string was shorter, but had no luck--nor patience.

You can see what I mean if you fix line 25:

$ cat 1133959.pl #! perl -slw use strict; . . . snip . . . print zipD( $A, $B ), zipD( $B, $A ); print zipR( $A, $B ), zipR( $B, $A ); . . . snip . . . $ perl 1133959.pl AaBbCcDdEeFGHIJAaBbCcDdEeFGHIJ AaBbCcDdEeFGHIJaAbBcCdDeEFGHIJ Rate rR Rr Dd dD rR 327095/s -- -2% -55% -55% Rr 334881/s 2% -- -54% -54% Dd 720854/s 120% 115% -- -0% dD 721504/s 121% 115% 0% --

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^5: Merge 2 strings like a zip [benchmark]
by BrowserUk (Patriarch) on Jul 09, 2015 at 22:25 UTC

    Try this:

    sub zipD($$) { my( $a, $b ) = (my $o = length( $_[0] ) >= length( $_[1] ) ) ? @_ +[ 0, 1 ] : @_[ 1, 0 ]; substr( $a, $_*2+$o, 0, substr( $b, $_, 1 ) ) for 0 .. length( $b +) -1; return $a; }

    Output:

    C:\test>1133857.pl AaBbCcDdEeFGHIJaAbBcCdDeEFGHIJ AaBbCcDdEeFGHIJaAbBcCdDeEFGHIJ Rate Rr rR Dd dD Rr 85339/s -- -1% -40% -41% rR 86543/s 1% -- -39% -40% Dd 142186/s 67% 64% -- -2% dD 145076/s 70% 68% 2% --

    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!

      BrowserUk:

      Beautiful!

      $ perl 1133959.pl AaBbCcDdEeFGHIJaAbBcCdDeEFGHIJ AaBbCcDdEeFGHIJaAbBcCdDeEFGHIJ Rate rR Rr Dd dD rR 334575/s -- -1% -53% -54% Rr 336364/s 1% -- -53% -53% Dd 713836/s 113% 112% -- -1% dD 720523/s 115% 114% 1% --

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.