J
John Small
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
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
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.
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
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
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
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.
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