Skip to content

Instantly share code, notes, and snippets.

@ZakriaJanjua
Created May 22, 2022 18:23
Show Gist options
  • Save ZakriaJanjua/e93bf8cf98d1d8c9aa6d0946a54c3fe8 to your computer and use it in GitHub Desktop.
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.
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