Skip to content

Instantly share code, notes, and snippets.

@reeddunkle
Created July 20, 2016 15:59
Show Gist options
  • Select an option

  • Save reeddunkle/6e922252f3d506d014b0b2588087321e to your computer and use it in GitHub Desktop.

Select an option

Save reeddunkle/6e922252f3d506d014b0b2588087321e to your computer and use it in GitHub Desktop.
An example of decoding RLE with itertools.groupby()
def decode(compressed):
'''
Given an Run Length Encoded (RLE) string, decodes it
into its original format, and returns this decoded string.
'''
groups = groupby(compressed, lambda x: x.isdigit())
output = ''
count = 1
for key, group in groups:
if key is True:
count = int(''.join(list(group)))
else:
letters = list(group)
output += (letters.pop(0) * count) + ''.join(letters)
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment