Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Created March 1, 2014 13:53
Show Gist options
  • Save mryoshio/9290050 to your computer and use it in GitHub Desktop.
Save mryoshio/9290050 to your computer and use it in GitHub Desktop.
compress, decompress
def decompress(args)
ans = []
args.each do |a|
if a.is_a? Array
ans.push a.last*a.first
else
ans.push a
end
end
ans.join("")
end
def compress(args)
tmp = []
ans = []
tmp.push args.shift
while args.any?
tmp.push args.shift unless ans.empty?
if args.first == tmp.last
tmp.push args.shift
else
if tmp.size == 1
ans.push tmp.last
else
ans.push [tmp.size, tmp.last]
end
tmp.clear
end
end
ans
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment