Untitled

 avatar
unknown
plain_text
5 months ago
921 B
3
Indexable
def solution(number):
    while True:
        new_number = []
        i = 0
        changed = False
        while i < len(number):
            j = i
            # Find the end of the run of identical digits
            while j < len(number) and number[j] == number[i]:
                j += 1
            run_length = j - i
            if run_length >= 2:
                # Calculate the sum of the digits in the run
                sum_digits = int(number[i]) * run_length
                new_number.append(str(sum_digits))
                changed = True
            else:
                # Single digit, append as is
                new_number.append(number[i])
            i = j
        # Form the new string after replacements
        new_number_str = ''.join(new_number)
        if not changed:
            # No changes made; the process is complete
            break
        number = new_number_str
    return number
Editor is loading...
Leave a Comment