Python Forum
How does this RLE code work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does this RLE code work?
#1
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
Reply
#2
# 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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't get graph code to work properly. KDDDC2DS 1 663 Sep-16-2024, 09:17 PM
Last Post: deanhystad
  I can't for the life of me get this basic If statement code to work CandleType1a 8 2,288 May-21-2024, 03:58 PM
Last Post: CandleType1a
  hi need help to make this code work correctly atulkul1985 5 1,950 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 1,542 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Beginner: Code not work when longer list raiviscoding 2 1,673 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 3,565 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 2,761 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 1,860 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 4,355 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  cannot get code to work Led_Zeppelin 10 4,580 Jun-30-2022, 06:28 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020