Skip to content

Instantly share code, notes, and snippets.

@ilkkao
Created July 22, 2014 13:56
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 ilkkao/476b525f3fcfa143a4f7 to your computer and use it in GitHub Desktop.
Save ilkkao/476b525f3fcfa143a4f7 to your computer and use it in GitHub Desktop.
def compress(input)
previous_char = nil
current_count = 0
output = ""
input.each_char do |c|
if c == previous_char || previous_char == nil
current_count += 1
elsif previous_char != nil
output << "#{current_count}#{previous_char}"
current_count = 1
end
previous_char = c
end
output << "#{current_count}#{previous_char}"
output
end
input = "AAABBCCCDDAA"
expected = "3A2B3C2D2A"
if compress(input) != expected
p compress(input)
p expected
fail
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment