Last active
May 19, 2016 00:51
-
-
Save bradherman/6447b6419fbbcd6f37036c97c683c2af to your computer and use it in GitHub Desktop.
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
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