Gavin Kistner
11/6/2006 3:51:00 AM
Giles Bowkett wrote:
> I have an array of arrays. the interior arrays can be of any arbitrary
> length including zero. I want to sort the array of arrays in such a
> way that the interior arrays with the most elements are closest to the
> middle of the main array.
main = (-4..12).map{ |i| (0..i).to_a }
sorted = main.sort_by{ |sub| sub.length }
i = 0
half1, half2 = sorted.partition{ (i+=1) % 2 == 0 }
pyramid = half1 + half2.reverse
require 'pp'
pp pyramid
#=> [[],
#=> [],
#=> [0, 1],
#=> [0, 1, 2, 3],
#=> [0, 1, 2, 3, 4, 5],
#=> [0, 1, 2, 3, 4, 5, 6, 7],
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8],
#=> [0, 1, 2, 3, 4, 5, 6],
#=> [0, 1, 2, 3, 4],
#=> [0, 1, 2],
#=> [0],
#=> [],
#=> []]