Created
May 22, 2022 18:23
-
-
Save ZakriaJanjua/e93bf8cf98d1d8c9aa6d0946a54c3fe8 to your computer and use it in GitHub Desktop.
Take a string as input and count the numbers of characters in their occurrence and return the result with their number of occurrences and the letter.
This file contains 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 countCharacters(string): | |
single = [] | |
whole = [] | |
result = "" | |
for i in string: | |
if (len(single) == 0): | |
single.append(i) | |
elif (i == single[0]): | |
single.append(i) | |
else: | |
whole.append(single) | |
single = [] | |
single.append(i) | |
if (len(single) != 0): | |
whole.append(single) | |
for i in whole: | |
result = result + "" + str(len(i)) + i[0] | |
return result | |
print(countCharacters('aaabcccdeefg')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment