Created
August 25, 2016 00:36
-
-
Save pzelnip/36d20a554c9195ddb1413a947c3f8c02 to your computer and use it in GitHub Desktop.
Counting letters in a string
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
# From https://www.linkedin.com/groups/25827/25827-6166706414627627011 | |
# "Today i did a simple test that you give a string to a function, and then the | |
# return output should count the letters sequencially; as following example: | |
# in: "aaaaabbbbccccccaaaaaaa" => out: "5a4b6c7a" | |
# | |
def func(input_string): | |
if not input_string: | |
return "" | |
if len(input_string) == 1: | |
return "1%s" % input_string | |
change_points = [] | |
start = 0 | |
for x in range(0, len(input_string) - 1): | |
if input_string[x] != input_string[x+1]: | |
change_points.append((start, x+1)) | |
start = x + 1 | |
change_points.append((start, len(input_string))) | |
return "".join(["%s%s" % (end - start, input_string[start]) for start, end in change_points]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment