Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Puzzle: need a more general algorithm

by Zaxo (Archbishop)
on Jul 08, 2002 at 20:12 UTC ( #180303=note: print w/ replies, xml ) Need Help??


in reply to Puzzle: need a more general algorithm

How about this? Sum the number of products over categories to get the total number of products. Divide that total by the number of columns to get the average height of a column. Shift categories off an array of unused ones and push them onto the current column if the new total would be closer to the average than the old. Keep a running average as a tiebreaker.

That may have corner cases which it doesn't optimize, but it should be good enough and run a whole lot faster than exhaustive search.

Update: Here is a corner case that doesn't optimize this way: @heights = qw/ 25 23 24 35 3 40/; $cols = 5; More attention to the running average could fix that, but I'm sure loopholes would remain.


Comment on Re: Puzzle: need a more general algorithm
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2014-12-27 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls