Skip to content

Instantly share code, notes, and snippets.

@alvinsj
Created March 14, 2012 12:34
Show Gist options
  • Save alvinsj/2036185 to your computer and use it in GitHub Desktop.
Save alvinsj/2036185 to your computer and use it in GitHub Desktop.
Group continuos sequences in an array
def group_sequence(seq)
return [] if seq.size == 0
g = Array.new
sub = Array.new
sub << seq[0]
if seq.size > 2
(seq[1..-1]).each do |s|
if s -sub[-1] == 1
sub << s
else
g << sub
sub = Array.new
sub << s
end
end
end
g << sub
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment