On Sun, 11 Jul 2004 16:10:34 +0900, 
Hal Fulton <hal9000 / hypermetrics.com> 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?

Here's one that doesn't follow that "need at least three of more"
limitation, but it's how *I* would do it, because it returns an array of
*only* ranges, which seems like it would be more fun to deal with that a
mix of ranges and numbers.

module Enumerable
  def to_ranges
    ranges = Array.new
    self.sort.each do |e|
      if ranges[-1] == nil or ranges[-1].end.succ != e
        ranges << Range.new(e,e)
        next
      end
      ranges[-1] = Range.new(ranges[-1].begin, e)
    end
    return ranges
  end
end

Jason Creighton