Skip to content

Instantly share code, notes, and snippets.

@StanDimitroff
Last active May 7, 2018 07:17
Show Gist options
  • Save StanDimitroff/f8321dbc709d4f480e38123c312277b9 to your computer and use it in GitHub Desktop.
Save StanDimitroff/f8321dbc709d4f480e38123c312277b9 to your computer and use it in GitHub Desktop.
Run-length encoding implementation in Python
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