Created
May 8, 2019 17:46
-
-
Save renzon/95e538e65672398500d03923ee8ec8ed to your computer and use it in GitHub Desktop.
contador
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
"""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