📅  最后修改于: 2022-03-11 15:04:49.699000             🧑  作者: Mango
# 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"