[TAG] Max-spread algorithm

Neil Youngman ny at youngman.org.uk
Thu Jul 5 11:28:19 MSD 2007


On or around Thursday 05 July 2007 08:06, Karl-Heinz Herrmann reorganised a 
bunch of electrons to form the message:
> On Wed, 4 Jul 2007 22:50:50 -0400
>
> Ben Okopnik <ben at linuxgazette.net> wrote:
> > Can you reproduce this algorithm? :) I found it a very interesting
> > exercise, myself.
>
> Hm.... line up all porks, start puting in one tomato in the gaps until
> tomate are out or alternatively gaps are out. If gaps are out start
> again at the beginning putting an additional tomato in the gaps. Repeat
> till tomatoes are gone....

That's potentially quite uneven, e.g. 8 pork cubes, 2 tomatoes, leaves all 
tomatoes at one end. I would try something like 

assert( pork_cubes > 1 );
gaps = pork_cubes - 1;
fill = tomatoes/gaps;
toms_used = 0;
for( g = 0; g < gaps; ++g )
{
  toms_in_gap[g] = ((g+1) *fill) - toms_used;
  toms_used += toms_in_gap[g];
}

I think this still biases a little towards one end, but it shouldn't be too 
hard to add a little tweak that fixes that.

Neil




More information about the TAG mailing list