Skip to content

Instantly share code, notes, and snippets.

@brainix
Last active February 7, 2021 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainix/5509c6e99481f9822586f531af688f0a to your computer and use it in GitHub Desktop.
Save brainix/5509c6e99481f9822586f531af688f0a to your computer and use it in GitHub Desktop.
Run-length encoding tech screen
# https://twitter.com/brainix/status/1358343863354552324
def runlength_encoding(s):
if s:
count = 1
for c1, c2 in zip(s[:-1], s[1:]):
if c1 == c2:
count += 1
else:
yield c1, count
count = 1
yield s[-1], count
print(list(runlength_encoding('aaaabbbcca')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment