Python Forum

Full Version: How does this RLE code work?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
There's this RLE code my teacher showed the class and I've tried to make sense of it but struggling. Could anyone explain to me how the code works, maybe by adding comments to each line.

Thanks in advance.
def RLE(text_to_encode):
    encoded_text = ""
    length = len(text_to_encode)
    if length == 0:
        encoded_text += ""
    elif length == 1:
        encoded_text += text_to_encode + str(length)
    else:
        count = 1
        i = 1
        while i < length:
        # Checks if it is the same letter.
            if text_to_encode[i] == text_to_encode[i - 1]: 
                count += 1
            else:
                encoded_text += text_to_encode[i - 1] + str(count)
                count = 1
            i += 1
        encoded_text += text_to_encode [i - 1] + str(count)
    return encoded_text
# given original function was not pythonic, so i refactored it a bit

def RLE(text_to_encode):
    # get the amount of characters in variable text_to_encode
    length = len(text_to_encode)
    
    # as there is nothing to encode we return an empty string
    if length == 0:
        return ""
    
    # as text_to_encode is only one character we return that added with a "1"
    elif length == 1:
        return f"{text_to_encode}1"
    
    # so text_to_encode is at least 2 or more characters long
    else:
        # initialize counter variable and storage for encoded string
        encoded_text = ""
        count = 1
        # we loop over all charcters starting at index 1 !
        for i in range(1, length):
            # we compare the character with the one before
            if text_to_encode[i] == text_to_encode[i - 1]:
                # and if it is the same we increase counter
                count += 1
            else:
                # characters are not the same
                # so we add character and string representation of its counter to encoded_text
                encoded_text = f"{encoded_text}{text_to_encode[i - 1]}{str(count)}"
                # and reset counter for next character
                count = 1
        
        # we add the last character that was counted
        encoded_text = f"{encoded_text}{text_to_encode[i - 1]}{str(count)}"
        # and give back the encoded_text
        return encoded_text

print(RLE("aaaaaaabbbbbbbbccccccccc"))
# Output => 'a7b8c9'