Skip to content

Instantly share code, notes, and snippets.

@jameshwc
Last active January 1, 2020 13:38
Show Gist options
  • Save jameshwc/a233613d012c4f03ae78d2ffe4e3c1c5 to your computer and use it in GitHub Desktop.
Save jameshwc/a233613d012c4f03ae78d2ffe4e3c1c5 to your computer and use it in GitHub Desktop.
LZW implementation via python
def Compress(s):
dictionary = {chr(i):i for i in range(0,255)} # create dictionary according to ascii code
last = 256 # the size of dictionary
p = ""
result = []
for c in s:
pc = p+c
if pc in dictionary:
p = pc # if pc in dictionary then try to append next character until pc is not in dictionary
else:
result.append(dictionary[p])
dictionary[pc] = last # save pc's code
last += 1 # increment the size of dictionary
p = c
if p != '':
result.append(dictionary[p])
return result
def Uncompress(arr):
dictionary = {i:chr(i) for i in range(0,255)}
last = 256
result = []
p = arr.pop(0) # get first element and pop it out
result.append(dictionary[p])
for c in arr:
if c in dictionary:
entry = dictionary[c]
result.append(entry)
dictionary[last] = dictionary[p] + entry[0] # append code
last += 1 # increment the size of dictionary
p = c # maintain the info of last character
return result
def get_test_str(filename):
with open(filename, 'r') as file:
return file.read()
if __name__ == '__main__':
test_str = get_test_str("google.html")
compress_str = Compress(test_str)
uncompress_str = Uncompress(compress_str)
# print(compress_str, uncompress_str)
print("filename: google_homepage.html")
print "len of test string:", len(test_str)
print "len of compressed string:", len(compress_str)
print "compression ratio:", float(len(compress_str))/len(test_str)
print "max element:", max(compress_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment