Array of indexes to array of ranges

前端 未结 7 944
执念已碎
执念已碎 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:10
    array = [505, 506, 507, 600, 601, 602, 603, 1643, 1644, 1645, 1646, 1647, 1648, 1649, 1650, 1651, 1652, 1653, 1654]
    array.inject([]){ |a, e| a[-1] && a[-1].last && a[-1].last == e-1 ? a[-1] = (a[-1].first..e) : a << (e..e); a }
    #=> [505..507, 600..603, 1643..1654]
    

    and for not sorted arrays you can presort it:

    array.sort!.uniq!
    
    0 讨论(0)
提交回复
热议问题