Skip to content

Instantly share code, notes, and snippets.

@bradherman
Last active May 19, 2016 00:51
Show Gist options
  • Save bradherman/6447b6419fbbcd6f37036c97c683c2af to your computer and use it in GitHub Desktop.
Save bradherman/6447b6419fbbcd6f37036c97c683c2af to your computer and use it in GitHub Desktop.
def rle(str)
current = {}
str.chars.each_with_object([]) do |char, arr|
if current.empty?
current = {"#{char}": 1}
elsif current.keys.first == char.to_sym
current[:"#{char}"] += 1
else
arr << current
current = {"#{char}": 1}
end
end << current
end
def rld(arr)
arr.each_with_object("") do |pair, str|
str << pair.keys.first.to_s * pair.values.first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment