Skip to content

Instantly share code, notes, and snippets.

@jfirebaugh
Forked from masverba/rle.rb
Created May 9, 2012 23:04
Show Gist options
  • Save jfirebaugh/2649585 to your computer and use it in GitHub Desktop.
Save jfirebaugh/2649585 to your computer and use it in GitHub Desktop.
Function to do run-length encoding on an Array
class Array
# See https://en.wikipedia.org/wiki/Run-length_encoding.
def rle
chunk {|e| e }.map {|e, a| [e, a.length] }
end
end
raise "Incorrect rle function." unless [1,1,1,2,3,3].rle == [
[1,3],
[2,1],
[3,2],
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment