Created
April 1, 2011 00:18
-
-
Save epitron/897535 to your computer and use it in GitHub Desktop.
runlength encode/decode binary strings
This file contains hidden or 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
s = "1100100001100001110111101110110011111010010000100101011110010110" | |
def rle(s) | |
s.gsub(/(0{3,9}|1{3,9})/) { "#{$1.size}#{$1.chars.first}" } | |
end | |
def rld(s) | |
s.gsub(/([3-9])([01])/) { $2 * $1.to_i } | |
end | |
p s | |
p rle(s) | |
p rld(rle(s)) | |
p [:works, s == rld(rle(s))] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment