Skip to content

Instantly share code, notes, and snippets.

@pulkit21
Created April 11, 2016 04:35
Show Gist options
  • Save pulkit21/aa856a1f9d7502e44bf55083494c89f3 to your computer and use it in GitHub Desktop.
Save pulkit21/aa856a1f9d7502e44bf55083494c89f3 to your computer and use it in GitHub Desktop.
Run length encoding
def run_encode(string)
print string.chars.chunk{|i| i}.map{|k, a| a.length == 1 ? k : k + a.length.to_s }.join
end
INPUT STRING
aaaaabbbbbbbbbccccpqrstuv
OUTPUT
a5b9c4pqrstuv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment