I'm new to Python, so maybe I'm asking for something very easy but I can't think of the problem in a Python way.
I have a compressed string. The idea is, if a character gets repeated 4-15 times, I make this change:
'0000' ---> '0|4'
If more than 15 times, I use a slash and two digits to represent the amount (working with hexadecimal values):
'00...(16 times)..0' ---> '0/10'
So, accustomed to other languages, my approach is the following:
def uncompress(line):
verticalBarIndex = line.index('|')
while verticalBarIndex!=-1:
repeatedChar = line[verticalBarIndex-1:verticalBarIndex]
timesRepeated = int(line[verticalBarIndex+1:verticalBarIndex+2], 16)
uncompressedChars = [repeatedChar]
for i in range(timesRepeated):
uncompressedChars.append(repeatedChar)
uncompressedString = uncompressedChars.join()
line = line[:verticalBarIndex-1] + uncompressedString + line[verticalBarIndex+2:]
verticalBarIndex = line.index('|') #next one
slashIndex = line.index('/')
while slashIndex!=-1:
repeatedChar = line[slashIndex-1:slashIndex]
timesRepeated = int(line[slashIndex+1:verticalBarIndex+3], 16)
uncompressedChars = [repeatedChar]
for i in range(timesRepeated):
uncompressedChars.append(repeatedChar)
uncompressedString = uncompressedChars.join()
line = line[:slashIndex-1] + uncompressedString + line[slashIndex+3:]
slashIndex = line.index('/') #next one
return line
Which I know it is wrong, since strings are inmutable in Python, and I am changing line contents all the time until no '|' or '/' are present.
I know UserString exists, but I guess there is an easier and more Pythonish way of doing it, which would be great to learn.
Any help?
line(the same as was the name of the old object). There's nothing wrong w/ that. Allstrremain immutable.compress = lambda s: s.encode('zip')anduncompress = lambda z: z.decode('zip')? It is inefficient to compress/uncompress byte-by-byte in pure Python