-
-
Save jfirebaugh/2649585 to your computer and use it in GitHub Desktop.
Function to do run-length encoding on an Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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