Detecting overlapping ranges in Ruby

前端 未结 4 1261
孤街浪徒
孤街浪徒 2021-01-03 06:50

I have array of ranges :

[[39600..82800], [39600..70200],[70200..80480]]

I need to determine if there is overlapping or not.What is an easy

4条回答
  •  臣服心动
    2021-01-03 07:49

    Is this not a way to do it?

    def any_overlapping_ranges(array_of_ranges)
       array_of_ranges.sort_by(&:first).each_cons(2).any?{|x,y|x.last>y.first}
    end
    
    p any_overlapping_ranges([50..100, 1..51,200..220]) #=> True
    

提交回复
热议问题