Skip to content

Instantly share code, notes, and snippets.

@epitron
Created April 1, 2011 00:18
Show Gist options
  • Save epitron/897535 to your computer and use it in GitHub Desktop.
Save epitron/897535 to your computer and use it in GitHub Desktop.
runlength encode/decode binary strings
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