From: Siep Korteling Date: 2008-08-07T05:25:32+09:00 Subject: Re: Need help detecting overlapping ranges Bryan Richardson wrote: > Hello all, > > I am writing some code where I create a bunch of ranges, then at the end > I want to create new ranges out of any ranges that overlap one another. > For example, say I have the following ranges: > > (1..5) (7..11) (22..29) (5..8) > > Given the ranges above, I want to end up with the following ranges: > > (1..11) (22..29) > (...) > This code works, but it is *EXTREMELY* slow (my current array of ranges > is averaging out to ~24000 range elements). Anyone have an idea of how > to speed it up? > > -- > Thanks! > Bryan Sets are usually much faster then large arrays and sets have a divide method. require 'set' ranges=[(1..5),(7..11),(22..29),(5..8)] set=Set.new ranges.each do|range| range.each do|el| set<, #}> hth, Siep -- Posted via http://www.ruby-forum.com/.