Skip to content

Instantly share code, notes, and snippets.

@LTe
Created April 17, 2010 11:39
Show Gist options
  • Save LTe/369491 to your computer and use it in GitHub Desktop.
Save LTe/369491 to your computer and use it in GitHub Desktop.
def LZW_decode(data)
data.map!{ |x| x.to_i }
d = {}
n = 7
d[1] = "a"
d[2] = "-"
d[3] = "h"
d[4]= "i"
d[5] = "s"
d[6] ="t"
pk = data[0]
result = [d[pk]]
for k in data[1..-1]
pc = d[pk]
if(d[k] != nil)
d[n] = pc + d[k][0].chr
n = n+1
result << d[k]
else
d[n] = pc + pc[0].chr
n = n+1
result << (pc + pc[0].chr)
end
pk = k
end
return result
end
data = "6 3 4 5 2 3 1 6 2 9 11 16 12 14 4 20 10 8 23 13".split(" ")
puts LZW_decode(data).to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment