Skip to content

Instantly share code, notes, and snippets.

@simplyvikram
Last active August 29, 2015 13:57
Show Gist options
  • Save simplyvikram/9589841 to your computer and use it in GitHub Desktop.
Save simplyvikram/9589841 to your computer and use it in GitHub Desktop.
def lzw_encode(text):
"""Compress a string to a list of output symbols."""
# Build the dictionary.
dict_size = 256
dictionary = dict((chr(i), i) for i in xrange(dict_size))
w = ""
result = []
for c in text:
wc = w + c
if wc in dictionary:
w = wc
else:
# print dictionary[w], '---', type(dictionary[w])
result.append(dictionary[w])
# Add wc to the dictionary.
dictionary[wc] = dict_size
dict_size += 1
w = c
# Output the code for w.
if w:
result.append(dictionary[w])
return result
def lzw_decode(lzw_output):
"""Decompress a list of output ks to a string."""
# Build the dictionary.
dict_size = 256
dictionary = dict((i, chr(i)) for i in xrange(dict_size))
w = result = chr(lzw_output.pop(0))
for k in lzw_output:
if k in dictionary:
entry = dictionary[k]
elif k == dict_size:
entry = w + w[0]
else:
raise ValueError('Bad encoding k: %s' % k)
result += entry
# Add w+entry[0] to the dictionary.
dictionary[dict_size] = w + entry[0]
dict_size += 1
w = entry
return result
def compress(uncompressed):
"""Compress a string to a list of output symbols."""
# Build the dictionary.
dict_size = 256
dictionary = dict((chr(i), i) for i in xrange(dict_size))
w = ""
result = []
for c in uncompressed:
wc = w + c
if wc in dictionary:
w = wc
else:
# print dictionary[w], '---', type(dictionary[w])
result.append(dictionary[w])
# Add wc to the dictionary.
dictionary[wc] = dict_size
dict_size += 1
w = c
# Output the code for w.
if w:
result.append(dictionary[w])
# return ''.join(result)
return result
def decompress(compressed):
"""Decompress a list of output ks to a string."""
# Build the dictionary.
dict_size = 256
dictionary = dict((i, chr(i)) for i in xrange(dict_size))
w = result = chr(compressed.pop(0))
for k in compressed:
if k in dictionary:
entry = dictionary[k]
elif k == dict_size:
entry = w + w[0]
else:
raise ValueError('Bad compressed k: %s' % k)
result += entry
# Add w+entry[0] to the dictionary.
dictionary[dict_size] = w + entry[0]
dict_size += 1
w = entry
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment