Last active
February 7, 2021 09:22
-
-
Save brainix/5509c6e99481f9822586f531af688f0a to your computer and use it in GitHub Desktop.
Run-length encoding tech screen
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
# 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