Created
March 1, 2014 13:53
-
-
Save mryoshio/9290050 to your computer and use it in GitHub Desktop.
compress, decompress
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
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