Skip to content

Instantly share code, notes, and snippets.

@lonelyelk
Created July 2, 2020 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lonelyelk/d0604fa143f9061556e291ed7f163186 to your computer and use it in GitHub Desktop.
Save lonelyelk/d0604fa143f9061556e291ed7f163186 to your computer and use it in GitHub Desktop.
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