DEV Community

loading...

Discussion on: Daily Challenge #93 - Range Extraction

Collapse
citizen428 profile image
Michael Kohl

Let's do Ruby today:

def solution(input)
  input
    .chunk_while { |i, j| i + 1 == j }
    .flat_map do |a|
      case a.size
      when 1 then a.first.to_s
      when 2 then [a.first.to_s, a.last.to_s]
      else "#{a.first}-#{a.last}"
      end
    end
end

solution([-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20])
#=> ["-6", "-3-1", "3-5", "7-11", "14", "15", "17-20"]