From: Martin DeMello Date: 2004-07-12T20:32:24+09:00 Subject: Re: A little algorithmic help requested... 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? a = [1,2,3,4,6,7,8,11,12,15,16,17] i = 0; b = [] while (a[i]) do j = i while (a[j] && (a[j] - a[i]) == (j-i)) do j += 1 end j = j-1 b += ((j-i < 2) ? a[i..j] : [a[i]..a[j]]) i = j+1 end p b #=> [1..4, 6..8, 11, 12, 15..17] martin