Ranges in ruby are pretty cool. I end up with arrays such as this:
geneRanges = [(234..25), (500..510), (1640..1653)]
And subsequently ha
Here's an answer (adapted from this code) that is more than twice as fast as the other code posted here. Further, only this answer and the one by @Steve handle arrays of non-integers.
class Array
def to_ranges
return [] if empty?
[].tap do |ranges|
init,last = first
each do |o|
if last && o != last.succ
ranges << (init..last)
init = o
end
last = o
end
ranges << (init..last)
end
end
end
Here are the benchmark results:
user system total real
steve 1.107000 0.000000 1.107000 ( 1.106221)
wayne 1.092000 0.000000 1.092000 ( 1.099220)
user229426 0.531000 0.000000 0.531000 ( 0.523104)
mladen1 0.780000 0.000000 0.780000 ( 0.774154)
mladen2 0.780000 0.000000 0.780000 ( 0.792159)
phrogz 0.218000 0.000000 0.218000 ( 0.220044)
All benchmarked code was adapted to remove sort.uniq for a fair comparison.