On Wed, 22 Oct 2008, John Small wrote: > I have a simple problem in Rails, I need to pack lists of things onto > the screen most efficiently. But it's not really a Rails problem it's a > simple packing algorithm. Well not really that simple since the general > packing algo is NP-complete, but in this case it's cut down to a very > simple case. So I'm wondering how to do it in Ruby. In a declarative > language like Prolog it's quite simple, but Ruby is mostly procedural so http://eigenclass.org/hiki.rb?tiny+prolog+in+ruby might be one way to solve it then > it's a bit fiddly. > > With all the irrelevant stuff taken out it boils down to this. I have an > array of numbers e,g. ar = [26,10,15,18,12] I want to pack those numbers > into neat little arrays like this end_result = [[26],[10,15],[17,12]]. > So that all the little sub-arrays are roughly the same length. in this You'd need to define "roughly". > case 26,25,29. The reason for I need this is that they're lists which I > want to put in three cols on the screen. But I want a fairly general > algorithm to do it. This looks to me like laying out columns in a newspaper or book so they balance. Maybe http://www.w3.org/People/howcome/TEB/www/hwl_th_10.html helps? > > Now ruby has some pretty cool stuff for dealing with arrays. Is there a > simple cool Ruby way to do this or do I have to use some clunky brute > force tactic? > > Thanks in advance > > John Small Hugh