Skip to content

Instantly share code, notes, and snippets.

@kissarat
Last active May 8, 2021 06:21
Show Gist options
  • Save kissarat/7a5d5d831e32ac1355124a47d959cc52 to your computer and use it in GitHub Desktop.
Save kissarat/7a5d5d831e32ac1355124a47d959cc52 to your computer and use it in GitHub Desktop.
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