From: Robert Klemme Date: 2004-07-11T23:27:26+09:00 Subject: Re: A little algorithmic help requested... "George Ogata" schrieb im Newsbeitrag news:877jtaltuz.fsf@optushome.com.au... > Hal Fulton writes: > > > 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] > > Interesting one to golf with: > > a.each_index{|i|a[i..j=i+2]==[x=a[i],x+1,x+2]and(0while a[j]+1==a[j+=1];a[i..j-=1]=a[i]..a[j])} Great! I was going to suggest the more conventional aa = [1,2,3,4,6,7,8,11,12,15,16,17] a = aa.dup ranges = [] first = last = a.shift a.each do |i| if i - last == 1 last = i else ranges << (first..last) first = last = i end end ranges << (first..last) p ranges robert