Last active
May 7, 2018 07:17
-
-
Save StanDimitroff/f8321dbc709d4f480e38123c312277b9 to your computer and use it in GitHub Desktop.
Run-length encoding implementation in Python
This file contains 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
from itertools import groupby | |
def encode(text): | |
""" | |
Returns the run-length encoded version of the text | |
(numbers after symbols, length = 1 is skipped) | |
""" | |
splitted = list(text) | |
grouped = [list(j) for i, j in groupby(splitted)] | |
encoded = '' | |
for group in grouped: | |
encoded += group[0] | |
encoded += str(len(group)) if len(group) > 1 else '' | |
return encoded |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment