Skip to content

Instantly share code, notes, and snippets.

@capttwinky
Created August 5, 2011 00:07
Show Gist options
  • Save capttwinky/1126633 to your computer and use it in GitHub Desktop.
Save capttwinky/1126633 to your computer and use it in GitHub Desktop.
"""easy string compression, based on the old compressed bitmap"""
class listComp(object):
@staticmethod
def compactLst(lstIn, lastOne = ''):
if lastOne == '':
listComp.myList = []
if lstIn[0] != lastOne:
#start a new acumulator
if lastOne and listComp.myList[-1][1] == 1:
del(listComp.myList[-1][1])
if len(lstIn) >1: listComp.myList.append([lstIn[0], 1])
else: listComp.myList.append([lstIn[0]])
else:
#increment the acumulator
listComp.myList[-1][1] += 1
if len(lstIn) >1:
listComp.compactLst(lstIn[1:], lstIn[0])
return listComp.myList
@staticmethod
def lstParse(lstIn):
lstOut = []
myMult = 1
for myLine in lstIn:
lineLex = []
for codePart in myLine.split(','):
if codePart.isdigit():
if lineLex and hasattr(lineLex[-1],'real'): myMult = int(codePart)
else: lineLex.append(int(codePart))
else:
lineLex.extend(list(codePart*myMult))
if myMult >1: myMult = 1
lineParse = []
for myBit in lineLex:
if myBit == '@':
lineParse.append("_/")
else:
lineParse.append(" "*myBit)
lstOut.append("".join(lineParse))
return lstOut
lstCoded = ['14,@,14,@,6,3,@,4,@', '5,3,@,8,3,@,2,4,@,2,@,4,@,2,@,4,2,@,6,3,@', '2,@,4,@,2,@,2,2,@,8,@,6,3,@,4,@,2,@,4,@,2,@,4,@', '1,@,4,@,2,@,6,2,@,4,@,6,@,4,@,2,@,2,@,4,@,2,@,4,@', '2,3,@,2,@,2,3,@,8,2,@,2,3,@,4,@,4,2,@,6,3,@', '5,@,56,@', '@,@,54,2,@,10']
for myLine in listComp.lstParse(lstCoded):
print myLine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment