Created
July 2, 2020 07:54
-
-
Save lonelyelk/d0604fa143f9061556e291ed7f163186 to your computer and use it in GitHub Desktop.
This file contains 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
s = "stttartuuuup" | |
def compress(str) | |
complex_object = str.chars.each_with_object({result: ""}) do |char, obj| | |
if obj[:current] == char | |
obj[:count] += 1 | |
next | |
elsif obj[:current] | |
obj[:result] += "#{obj[:count]}x#{obj[:current]}" | |
end | |
obj[:count] = 1 | |
obj[:current] = char | |
end | |
complex_object[:result] + "#{complex_object[:count]}x#{complex_object[:current]}" | |
end | |
puts compress(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment