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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

G'day Dave,

By alternatively splicing elements from either end of the distribution, I get your original 'qw( A C B A D C A B C A )'.

$ perl -Mstrict -Mwarnings -E ' my %bag = ( A => 4, B => 2, C => 3, D => 1 ); my @distribution; for my $key (sort { $bag{$b} <=> $bag{$a} } keys %bag) { my $base_offset = int(@distribution / ($bag{$key} + 1)); my $offset = $base_offset; for (1 .. $bag{$key}) { next unless $_ % 2; splice @distribution, $offset, 0, $key; if ($_ < $bag{$key}) { splice @distribution, -$offset, 0, $key; } $offset += $base_offset + 1; } } say "@distribution"; ' A C B A D C A B C A

If I round up the $base_offset value, the middle two elements are reversed but everything else remains the same. I don't see this as being more or less uniform but maybe it's more correct.

$ perl -Mstrict -Mwarnings -E ' my %bag = ( A => 4, B => 2, C => 3, D => 1 ); my @distribution; for my $key (sort { $bag{$b} <=> $bag{$a} } keys %bag) { my $base_offset = int(@distribution / ($bag{$key} + 1) + 0.5); + my $offset = $base_offset; for (1 .. $bag{$key}) { next unless $_ % 2; splice @distribution, $offset, 0, $key; if ($_ < $bag{$key}) { splice @distribution, -$offset, 0, $key; } $offset += $base_offset + 1; } } say "@distribution"; ' A C B A C D A B C A

I haven't done extensive testing on this solution. Beyond your sample input, I tried:

my %bag = ( A => 4, B => 2, C => 3, D => 1, Z => 1 );

gives: A C B A C D Z A B C A; and

my %bag = ( A => 4, B => 2, C => 3, D => 1, Y => 5, Z => 1 );

gives: Y A C Y B A C Z D Y A B Y C A Y; and

my %bag = ( A => 4, B => 2, C => 3, D => 1, X => 6, Y => 5, Z => 1 );

gives: X Y A X C Y B A X Y Z D C X A B Y C X A Y X; and

my %bag = ( A => 4, B => 2, C => 3, D => 1, W => 4, X => 6, Y => 5, Z +=> 1 );

gives: X Y A W X C Y B A W X Y Z D C X W A B Y C X W A Y X

The above 4 tests all included rounding; without rounding, the results are:

A C B A Z D C A B C A Y A C Y B A C D Z Y A B Y C A Y X Y A C X Y B A C X D Z Y X A B Y X C A Y X X Y A W C X Y B A W C X D Z Y X W A B Y X C W A Y X

-- Ken


In reply to Re: Bag uniform distribution algorithms by kcott
in thread Bag uniform distribution algorithms by davido

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (4)
    As of 2014-09-21 00:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (165 votes), past polls