Created
November 29, 2016 02:07
-
-
Save jovianlin/c25f23cc1bd360170a74c06e1be4c241 to your computer and use it in GitHub Desktop.
Convert "aaaabbbbbcccccde" to "a4b5c5d1e1"
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 foo(s): | |
if len(s) <= 0: | |
return None | |
else: | |
output, curr_char, curr_count = '', '', 0 | |
for idx in range(0, len(s)): | |
if s[idx] == curr_char: | |
curr_count += 1 | |
else: | |
output += curr_char + str(curr_count) if curr_count > 0 else curr_char | |
curr_char, curr_count = s[idx], 1 | |
if idx == len(s)-1: | |
output += curr_char + str(curr_count) | |
return output | |
if __name__ == '__main__': | |
test_s = 'aaaabbbbbcccccde' | |
print test_s | |
print foo(test_s) # outputs "a4b5c5d1e1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment