From: CT Date: 2004-07-11T17:22:53+09:00 Subject: Re: A little algorithmic help requested... Hiya, I may not have read you correctly, but does this do what you seek? -- require 'pp' a = [1,2,3,4,6,7,8,11,12,15,16,17] pp a.inject([[a.shift]]) {|arr,i| temp = arr.last if(temp.last.next == i) temp << i else arr << [i] end arr }.inject([]) {|arr,i| if(i.length >= 3) arr<<(i.first..i.last) else arr = arr + i end } -- Gives me: [1,2,3,4,6,7,8,11,12,15,16,17] First inject collects all consecutive ranges into sub-arrays, second one converts these to ranges/flattened arrays. corrections/improvements are welcome. HTH - CT On Sun, 11 Jul 2004 16:10:34 +0900, Hal Fulton wrote: > Here's a problem my tired brain is having trouble with. > > Given a sorted array of integers, convert them into as many > ranges as possible (ranges of three or more). > > Example: > [1,2,3,4,6,7,8,11,12,15,16,17] ==> [1..4,6..8,11,12,15..17] > > How would *you* do this? > > Thanks, > Hal > >