Skip to content

Instantly share code, notes, and snippets.

@LTe
Created April 17, 2010 20:16
Show Gist options
  • Save LTe/369777 to your computer and use it in GitHub Desktop.
Save LTe/369777 to your computer and use it in GitHub Desktop.
def lz78_encode(data)
slownik ={}
n = 1
c = ''
result = []
for s in data
if (!slownik.include?(c+s))
if c == ''
# symbol 's' nie występuje jeszcze w słowniku
result << " (0 , #{s}) "
slownik[s] = n
else
# ciąg 'c' jest w słowniku
result << " (#{slownik[c]} , #{s}) "
slownik[c+s] = n
end
n = n + 1
c = ''
else
c = c+s
end
end
return result
end
data = "abbbcaabbcbbcaaac".split(//)
puts lz78_encode(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment