Array of indexes to array of ranges

前端 未结 7 963
执念已碎
执念已碎 2020-12-09 12:46

Ranges in ruby are pretty cool. I end up with arrays such as this:

geneRanges = [(234..25), (500..510), (1640..1653)]

And subsequently ha

7条回答
  •  情书的邮戳
    2020-12-09 13:09

    ar=[505, 506, 507, 600, 1647, 1648, 1649, 1650, 1651, 1654]
    def to_range(a)
      s=a[0]
      a.each_cons(2) do |a|
        if a[1]-a[0]!=1
            p s .. a[0]
            s=a[1]
        end
      end
      left=a.index(s)
      p a[left..-1][0]..a[left..-1][-1]
    end
    to_range(ar)
    

提交回复
热议问题