-
-
Save lovasoa/4eaf5bff4f83fe7fe67cd5bc1f4fed30 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
def rle(s): | |
encode = "" # La variable qui va contenir le résultat | |
count = 0 | |
current_char = "" | |
for c in s: | |
if c == current_char: | |
count += 1 | |
else: # On a rencontré un nouveau caractère | |
if count > 0: | |
# On l'ajoute à la chaine de sortie | |
encode += str(count) + current_char | |
# On réinitialise count et current_char | |
count = 1 | |
current_char = c | |
# On ajoute le dernier caractère | |
encode += str(count) + current_char | |
return encode | |
if __name__ == "__main__": | |
print(rle(input("Texte? "))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment