Skip to content

Instantly share code, notes, and snippets.

@shonenada
Created June 26, 2014 08:39
Show Gist options
  • Save shonenada/faa1e4393ded92af90af to your computer and use it in GitHub Desktop.
Save shonenada/faa1e4393ded92af90af to your computer and use it in GitHub Desktop.
class LZ(object):
def __init__(self):
self.char_dict = list()
def clean(self):
self.char_dict = []
self.char_dict.append('')
def init_dict(self, string):
for char in string:
if char not in self.char_dict:
self.char_dict.append(char)
def encode(self, string):
self.clean()
self.init_dict(string)
output = ''
prefix = ''
for char in string:
temp = prefix + char
if temp in self.char_dict:
prefix += char
else:
output += str(self.char_dict.index(prefix))
self.char_dict.append(prefix + char)
prefix = char
return output
encoder = LZ()
print encoder.encode("ABCABABAA")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment