in reply to Re^3: Spreading out the elements
in thread Spreading out the elements

interleave(1, 2); ABB # BAB and BBA are also fine
[...] given that "BAB is also acceptable" decided that you might prefer the most diffuse spread possible regardless of whether that meant starting with an 'a' or a 'b'.

My fault; I should have made it clear that it was an edge case. However... even with the changes you've suggested, I get this:

print interleave(4, 5); ababbbaba print interleave(4, 7); ababbbbbaba

This should be more like

print interleave(4, 5); abbabbaba print interleave(4, 7); abbbabbabba
This version provides the maximum distance possible between all 'a's.

Replies are listed 'Best First'.
Re^5: Spreading out the elements
by BrowserUk (Patriarch) on Jul 05, 2007 at 06:47 UTC

    Ah, shame! That destroys the symmetry I was exploiting and so requires two lots of not quite identical code :(

    sub interleave { my( $a, $b ) = qw[ a b ]; my( $as, $bs ) = @_; return $a x $as . $b x $bs unless $as >1 and $bs; if( $as > $bs ) { ++$bs; my $aPerB = int( $as / $bs ); my $aRem = $as - $bs * $aPerB; my @as = ( $a x $aPerB ) x $bs; my $n = 0; $as[ $n ] .= $a, $as[ - ++$n ] .= $a, $aRem -= 2 while $aRem > + 1; $as[ @as / 2 ] .= $a if $aRem > 0; return join $b, @as; } else { --$as; my $bPerA = int( $bs / $as ); my $bRem = $bs - $as * $bPerA; my @bs = ( $b x $bPerA ) x $as; my $n = 0; $bs[ $n ] .= $b, $bs[ - ++$n ] .= $b, $bRem -= 2 while $bRem > + 1; $bs[ @bs / 2 ] .= $b if $bRem > 0; return $a . join( $a, @bs ) . $a; } } __END__ C:\test>624887 -S=1 -N=50 4a 5b: abbababba 4a 7b: abbabbbabba ----------- 4a 13b: abbbbabbbbbabbbba 4a 14b: abbbbbabbbbabbbbba 4a 15b: abbbbbabbbbbabbbbba 4a 16b: abbbbbabbbbbbabbbbba 4a 17b: abbbbbbabbbbbabbbbbba 4a 18b: abbbbbbabbbbbbabbbbbba 4a 19b: abbbbbbabbbbbbbabbbbbba ----------- 13a 4b: aaabaabaaabaabaaa 14a 4b: aaabaaabaabaaabaaa 15a 4b: aaabaaabaaabaaabaaa 16a 4b: aaabaaabaaaabaaabaaa 17a 4b: aaaabaaabaaabaaabaaaa 18a 4b: aaaabaaabaaaabaaabaaaa 19a 4b: aaaabaaaabaaabaaaabaaaa -----------

    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.
      > Ah, shame! That destroys the symmetry I was exploiting and so requires two lots of not quite identical code :(

      Oh, but it's _beautiful_ in terms of function. :) I'm not going to put it up on my wall, but I'll certainly keep it around in my "nifty and useful Perl stuff" folder; I'm sure it'll come in handy in the future. Heck, I'm about to use it right now to sort a bunch of domains for "whois" queries - .orgs need a 20-second delay between checks (otherwise they get rate-limited), so I'm going to "spread" the .orgs among all the other types and reduce the wait. Thanks again!