Created
April 17, 2010 20:16
-
-
Save LTe/369777 to your computer and use it in GitHub Desktop.
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 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