Skip to content

Instantly share code, notes, and snippets.

@jaydorsey
Last active June 19, 2018 15:29
Show Gist options
  • Save jaydorsey/27faee71c908073ef2c380c73682f95e to your computer and use it in GitHub Desktop.
Save jaydorsey/27faee71c908073ef2c380c73682f95e to your computer and use it in GitHub Desktop.
Run length encoding in Ruby
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW".
chars.
chunk_while { |before, after| before == after }.
reduce(String.new) { |memo, group| memo << "#{group.size}#{group.first}" }
rle = -> (str) { str.chars.chunk_while{ |b, a| b == a }.reduce(""){ |m, g| m << g.length.to_s << g.first } }
rle.call("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment