From: Bryan Richardson Date: 2008-08-07T07:25:20+09:00 Subject: Re: Need help detecting overlapping ranges Hi David, I like the conciseness of your code, but I think it only works for when the end of one range is the same as the beginning of another. I need to also handle the times when ranges overlap more than that (i.e. 1..5, 3..6 -> 1..6). Thanks for thinking about this! -- Thanks! Bryan David A. Black wrote: > Hi -- > > On Thu, 7 Aug 2008, Bryan Richardson wrote: > >> outages = Array.new >> break >> end >> end >> end >> outages << range >> end >> return outages >> end > > I did something similar in trying to implement Martin's algorithm. I > haven't tested it beyond eyeballing the results for this one run: > > ranges = [(1..5), (7..11), (22..29), (5..8)].sort_by {|r| r.first } > outages = [ranges.shift] > > ranges.each do |r| > if outages[-1].include?(r.first) > outages[-1] = Range.new(outages[-1].first, r.last) > else > outages.push(r) > end > end > > p outages > > > David -- Posted via http://www.ruby-forum.com/.