Skip to content

Instantly share code, notes, and snippets.

@jovianlin
Created November 29, 2016 02:07
Show Gist options
  • Save jovianlin/c25f23cc1bd360170a74c06e1be4c241 to your computer and use it in GitHub Desktop.
Save jovianlin/c25f23cc1bd360170a74c06e1be4c241 to your computer and use it in GitHub Desktop.
Convert "aaaabbbbbcccccde" to "a4b5c5d1e1"
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
print foo(test_s) # outputs "a4b5c5d1e1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment