Skip to content

Instantly share code, notes, and snippets.

@jmoon90
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save jmoon90/8922180 to your computer and use it in GitHub Desktop.

Select an option

Save jmoon90/8922180 to your computer and use it in GitHub Desktop.
def compress(sequence)
array = sequence.split
hash = Hash.new(0)
second_hash = Hash.new(0)
array.each_with_index do |item, i|
if hash[item] != 0 && array[i-1] != item
second_hash[item] += 1
else
hash[item] += 1
end
end
output(hash, second_hash)
end
def output(hash, second_hash)
hash.each do |k, v|
print "#{v} #{k} "
end
second_hash.each do |k, v|
print "#{v} #{k}"
end
puts
end
compress("40 40 40 40 29 29 29 29 29 29 29 29 57 57 92 92 92 92 92 86 86 86 86 86 86 86 86 86 86")
compress("73 73 73 73 41 41 41 41 41 41 41 41 41 41")
compress("1 1 3 3 3 2 2 2 2 14 14 14 11 11 11 2")
compress("7")
#output
#4 40 8 29 2 57 5 92 10 86
#4 73 10 41
#2 1 3 3 4 2 3 14 3 11 1 2
#1 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment