Skip to content

Instantly share code, notes, and snippets.

@jish
Created August 23, 2011 02:43
Show Gist options
  • Select an option

  • Save jish/1164205 to your computer and use it in GitHub Desktop.

Select an option

Save jish/1164205 to your computer and use it in GitHub Desktop.
Ranges
input = [1, 2, 3, 8, 9, 10, 21, 22, 23, 24]
def scan(input)
min, max = input[0], input[1]
output = []
input.each_with_index do |current, index|
next if index == 0
previous = input[index-1]
if (current - previous == 1)
# continue
else
output << "#{min}..#{previous}"
min = current
end
end
output << "#{min}..#{input.last}"
output
end
output = scan(input)
puts output.inspect
# => ["1..3", "8..10", "21..24"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment