Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Spreading out the elements

by fenLisesi (Priest)
on Jul 04, 2007 at 18:27 UTC ( [id://624922]=note: print w/replies, xml ) Need Help??


in reply to Spreading out the elements

use strict; use warnings; use Test::More; use POSIX; my @TESTS = ( [(0, 0) => undef], [(0, 1) => undef], [(1, 0) => undef], [(1, 1) => 'AB'], [(2, 1) => 'ABA'], [(3, 1) => { map {$_=>1} qw(ABAA AABA)}], [(4, 1) => { map {$_=>1} qw(ABAAA AABAA AAABA)}], [(1, 2) => { map {$_=>1} qw(ABB BBA BAB)}], [(2, 2) => 'ABBA'], [(7, 6) => 'ABABABABABABA'], [(3, 3) => { map {$_=>1} qw(ABBABA ABABBA)}], [(4, 14) => { map {$_=>1} qw( ABBBBABBBBBABBBBBA ABBBBBABBBBABBBBBA ABBBBBABBBBBABBBBA )}], ); plan tests => scalar @TESTS; for my $test (@TESTS) { my $skewer; my ($n_a, $n_b, $expected) = @$test; eval { $skewer = interleave( $n_a, $n_b ); }; if ($@) { ok( !defined( $expected ), "($n_a, $n_b) => undef" ); next; } if (ref $expected) { ok( exists $expected->{ $skewer }, "($n_a, $n_b) => $skewer" ); } else { ok( $expected eq $skewer, "($n_a, $n_b) => $skewer" ); } } exit( 0 ); ##-------------------------------------------------------------------+ sub interleave { my ($n_a, $n_b) = @_; if ($n_a < 1 or $n_b < 1) { die "Bad args to interleave: ($n_a, $n_b)"; } if ($n_a == 1) { return 'A' . 'B' x $n_b; } my $min_b = floor( $n_b / ($n_a - 1) ); my $leftover_b = $n_b - $min_b * ($n_a - 1); ##warn "($n_a, $n_b) => min_b:$min_b, lefto:$leftover_b"; my $skewer = 'A'; for (1..$leftover_b) { $skewer .= 'B' x ($min_b + 1); $skewer .= 'A'; } for ($leftover_b + 1 .. $n_a - 1) { $skewer .= 'B' x $min_b; $skewer .= 'A'; } ##warn "returning ($n_a, $n_b) => $skewer"; return $skewer; }
prints:
1..12 ok 1 - (0, 0) => undef ok 2 - (0, 1) => undef ok 3 - (1, 0) => undef ok 4 - (1, 1) => AB ok 5 - (2, 1) => ABA ok 6 - (3, 1) => ABAA ok 7 - (4, 1) => ABAAA ok 8 - (1, 2) => ABB ok 9 - (2, 2) => ABBA ok 10 - (7, 6) => ABABABABABABA ok 11 - (3, 3) => ABBABA ok 12 - (4, 14) => ABBBBBABBBBBABBBBA

Replies are listed 'Best First'.
Re^2: Spreading out the elements
by clinton (Priest) on Jul 05, 2007 at 00:49 UTC
    1..12 ok 1 - (0, 0) => undef # Growing up in Sweden ok 2 - (0, 1) => undef ok 3 - (1, 0) => undef ok 4 - (1, 1) => AB # The early years ok 5 - (2, 1) => ABA # still experimental ok 6 - (3, 1) => ABAA ok 7 - (4, 1) => ABAAA # Eurovision ok 8 - (1, 2) => ABB # Waterloo, Mama Mia ok 9 - (2, 2) => ABBA # Dancing Queen ok 10 - (7, 6) => ABABABABABABA ok 11 - (3, 3) => ABBABA # All falls apart ok 12 - (4, 14) => ABBBBBABBBBBABBBBA # Tribute bands abound
Re^2: Spreading out the elements
by oko1 (Deacon) on Jul 04, 2007 at 19:04 UTC
    [applause]

    Way too cool. :) Thank you!

    Did you use some existing algorithm, or did you come up with this on your own? Kudos either way, but I'm really curious.

      There isn't much of an algorithm, really. There are na-1 slots into which the B's will go. In the general case, some slots will have
      floor( nb / (na-1) )
      B's and others will have one more. Still, it took me more than an hour from start to finish. Cheers.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-25 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found