Created
July 20, 2016 15:59
-
-
Save reeddunkle/6e922252f3d506d014b0b2588087321e to your computer and use it in GitHub Desktop.
An example of decoding RLE with itertools.groupby()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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