Skip to content

Instantly share code, notes, and snippets.

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