Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
contador
"""Função que conta os caracteres de uma string
Ex:
>>> contar_caracteres('renzo')
e:1
n:1
o:1
r:1
z:1
>>> contar_caracteres('banana')
a:3
b:1
n:2
:param s: string a ser contada
"""
def contar_caracteres(s):
caracter_ordenados = sorted(s)
caracteres_anterior = caracter_ordenados[0]
contagem = 1
for caracter in caracter_ordenados[1:]:
if caracter == caracteres_anterior:
contagem += 1
else:
print(f'{caracteres_anterior}:{contagem}')
caracteres_anterior = caracter
contagem = 1
print(f'{caracteres_anterior}:{contagem}')
if __name__ == 'main':
contar_caracteres('renzo')
print()
contar_caracteres('banana')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment