Last active
May 8, 2021 06:21
-
-
Save kissarat/7a5d5d831e32ac1355124a47d959cc52 to your computer and use it in GitHub Desktop.
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
def compression(input_str): | |
def _compression(_input_str): | |
i = -1 | |
counter = 1 | |
for char in _input_str: | |
i += 1 | |
if i == 0: | |
continue | |
previous = _input_str[i - counter] | |
if previous == char: | |
counter += 1 | |
elif counter > 1: | |
yield previous + str(counter) | |
counter = 1 | |
else: | |
yield char | |
if counter > 1: | |
yield _input_str[i - 1] + str(counter) | |
return ''.join(_compression(input_str)) | |
print(compression("bbbbcccrrrr rrrr")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment