Skip to content

Instantly share code, notes, and snippets.

@maliqq
Created March 30, 2013 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maliqq/5278058 to your computer and use it in GitHub Desktop.
Save maliqq/5278058 to your computer and use it in GitHub Desktop.
a = [1,2,3,5,6,7,10,11,12,14,15]
def split_gaps(a)
gaps = []
buffer = a.inject([]) { |buffer, item|
prev = buffer[buffer.size - 1]
if prev && item - prev > 1
gaps << buffer
[]
else
buffer << item
buffer
end
}
gaps << buffer
end
puts split_gaps(a).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment