RLE encoding without count() method

I am trying to take a string as an input, and then output each letter with a repetition value. For example: aaab should output 3a1b. But I cannot use count() to do this.

I have tried to code it, but I am confused because I have used more for loops and if statements than my brain can process:

def rle_encode(str):
    count = 1
    for i in range(len(str)):
        if i != len(str):
            if str[i] == str[i+1]:
                count += 1
                str = str[i].replace(str[i],'')
                str = str[i+1].replace(str[i+1],'')
                print(str(count) + str(str[i]))


I get the following error for the line str = str[i+1].replace(str[i+1],''):

IndexError: string index out of range

1 answer

  • answered 2019-02-10 12:47 roganjosh

    We can implement a dictionary for a counter. Using get, we can return 0 if the key doesn't exist and increment from there:

    my_string = 'aaabb'
    my_counter = {}
    for item in my_string:
        my_counter[item] = my_counter.get(item, 0) + 1
    output = ''
    for k, v in my_counter.items():
        output += str(v) + k

    I need to come up with more elegant string building because you shouldn't really concatenate in a loop.

    Edit. To avoid the string concatenation in a loop, we can use a list comprehension. I'll leave my initial loop because it does the same thing but highlights a complexity issue and the thought process for eliminating it:

    output = ''.join([str(v) + k for k, v in my_counter.items()])