Created
November 30, 2015 07:49
-
-
Save rahbirul/5f008ac2b1e85a8b3a38 to your computer and use it in GitHub Desktop.
aaabbbccc -> a3b3c3
This file contains hidden or 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 compress(input): | |
if len(input) == 0: | |
return "" | |
output = [] | |
count = 1 | |
prev = input[0] | |
for c in input[1:-1]: | |
if c == prev: | |
count += 1 | |
else: | |
output.append(prev) | |
output.append(str(count)) | |
count = 1 | |
prev = c | |
if input[-1] == input[-2]: | |
count += 1 | |
else: | |
output.append(input[-2]) | |
output.append(str(count)) | |
count = 1 | |
output.append(input[-2]) | |
output.append(str(count)) | |
return ''.join(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment