Intermediate · Text processing · 151 of 264
Run-length encoding
Compress a word by writing each run of the same letter as the letter and how many times it repeats.
Input: a word. Output: e.g. aaabccdddd -> a3b1c2d4
Examples
Input
aaabccdddd
Output
a3b1c2d4
Input
abc
Output
a1b1c1
Input
zzzzzzzzzzzz
Output
z12
One more test checks other inputs when you press Check.
Stuck? The idea behind text processing
Go through text a character at a time, and build the answer as you go: a new string, or a list of pieces joined at the end with "".join().
ord() gives a character's number and chr() the character for a number: useful for shifting letters.
text = input()
out = []
for ch in text:
out.append(ch.upper() if ch.islower() else ch.lower())
print("".join(out))Watch for: Shifting letters past "z" must wrap round to "a": use % 26 on the letter's position in the alphabet.