Created
November 26, 2012 12:00
-
-
Save jmeirow/4147857 to your computer and use it in GitHub Desktop.
create array of ranges from array of individual values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pp' | |
require 'date' | |
def to_range low, high, input, output | |
if high > input.length | |
return output.each_slice(2).map {|a| a }.map { |x| x[0]..x[1]} | |
end | |
if low == 0 | |
output = Array.new | |
output << input[low] | |
else | |
if input[low]+1 != input[high] | |
output << input[low] | |
output << input[high] if input[high] | |
end | |
end | |
to_range (low+1), (high+1), input, output | |
end | |
input = [1,2,3,4,5,100,101,102,400,401,402,403,404,405,406] | |
pp to_range 0, 1, input, nil # => [1..5, 100..102, 400..406] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment