Nov-21-2019, 08:40 AM
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.
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