Created
January 13, 2018 08:08
-
-
Save jacobat/19e4519554e2928f9b029ecf2f7f9703 to your computer and use it in GitHub Desktop.
This file contains 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
def group_continuous(io) | |
result = [] | |
range_start = nil | |
offset = 0 | |
io.each_line do |line| | |
id = line.to_i | |
puts "#{id}\t#{range_start}\t#{offset}" | |
if !range_start.nil? && id == range_start + offset + 1 | |
offset = offset + 1 | |
else | |
if !range_start.nil? | |
result << (range_start..(range_start + offset)) | |
range_start = id | |
offset = 0 | |
else | |
range_start = id | |
offset = 0 | |
end | |
end | |
end | |
result << (range_start..(range_start + offset)) | |
result | |
end | |
io = StringIO.new(<<-EOS | |
1 | |
2 | |
3 | |
5 | |
6 | |
8 | |
11 | |
12 | |
13 | |
15 | |
16 | |
EOS | |
) | |
p group_continuous(io) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment