📌  相关文章
📜  用短划线替换范围内第一个和最后一个之间的连续数字 - Ruby 代码示例

📅  最后修改于: 2022-03-11 15:04:49.699000             🧑  作者: Mango

代码示例1
# Replacing consecutive numbers with dash between first and last in a range
def range_extraction(list)
  list.chunk_while {|i, j| i+1 == j }.map do |a|
    if a.size > 2
      a.first.to_s + "-" + a.last.to_s
    else
      a  
    end
  end.join(',')
end

print range_extraction([-3,-2,-1,2,10,15,16,18,19,20])  # "-3--1,2,10,15,16,18-20"