2.14. Run-length Encoding 2

Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and the character. For example, the string “AAAABBBCCDAA” would be encoded as “4A3B2C1D2A”.

Write a program to run-length encode a given string and print the result. The input string will have no digits and has only uppercase alphabetic characters.

Sample I/O:

Input:
AAAABBBCCDAA

Output:
4A3B2C1D2A

Input:
AABBA

Output:
2A2B1A
s = input()

cursor = ""       # to keep the current character
streak = 1
buildup = ""
for a in s:
    if a != cursor:
        if cursor != "":
            buildup += str(streak) + cursor     # notice the casting int to string
        cursor = a
        streak = 1
    else:
        streak += 1
buildup += str(streak) + cursor

print(buildup)