Skip to content

Instantly share code, notes, and snippets.

@jacobat
Created January 13, 2018 08:08
Show Gist options
  • Save jacobat/19e4519554e2928f9b029ecf2f7f9703 to your computer and use it in GitHub Desktop.
Save jacobat/19e4519554e2928f9b029ecf2f7f9703 to your computer and use it in GitHub Desktop.
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