Search code examples
pythonpython-3.xrle

Can you explain me the RLE algorithm code in python


I I've finally found how to make a RLE algorithm by watching a tutorial but This tutorial didn' t explain something in that code I didn't get why we write j = i instead of j = 0 (Knowing that I = 0) it's the same no ?

I didn't get why i = j + 1 either. Why i = j + 1 At the end of the function ? Why not simply i += 1 but if we want to repeat a loop in a loop then we do j + 1 ?

Did the first while loop is supposed to repeat the second while loop until the string is finished ?

And finally why encoded_message is repeated two times ? instead of one. We return encoded_message so that's it ? We can simply do print(encode(text)) instead of "print('The encoded message is the output ',encoded_message)" (when we put encode(text) into encoded_message)

I know i'm asking a lot of questions but I just can't memorize the code without understanding it, it would be totally useless and unproductive

    def encode(message):
        
        encoded_message = ""
        i = 0
        while(i<len(message)):
            count = 1
            ch = message[i]
            j = i # ???
            while(j<len(message)-1): # GET IT -----------------------------------------------------------
                if message[j] == message[j+1]: # if the previous and next characters are the same 
                    
                    count = count + 1 # we increase count variable
                    j += 1 # we increase j position
                    # GET IT ----------------------------------------------------------------------------
                else:
                    break
                
            encoded_message = encoded_message + str(count) + ch # "" + count converted to string + character (ch)
            i = j + 1 # ???
        return encoded_message


text = input('enter your charcter chain...')
encoded_message = encode(text)
print('The encoded message is the output ',encoded_message)

When I replaced j = i by j = 0 nothing is displayed in the terminal

see : no result


Solution

  • There is an outer loop and an inner loop. The outer loop with the variable i starts iterating over the message. The inner loop uses the variable j and starts at the current position of i.

    That is: when i=0 then j=0. But when i=5 (for example) then j=5 also.

    The inner loops task is to check whether 2 or more identical characters follow one another. If they do i is increased accordingly at the end of the inner loop. So that each letter of the message is only looked at once.

    That is why j should not be set to a constant value. Setting it to j=0 would cause the inner loop to start at the beginning of the message at every iteration.

    I added two simple print() statements to your code to clarify:

    def encode(message):
         encoded_message = ""
         i = 0
         while(i<len(message)):
             print(f'outer loop: i={i}')
             count = 1
             ch = message[i]
             j = i
             while(j<len(message)-1):
                 print(f'\tinner loop: j={j}')
                 if message[j] == message[j+1]: # if the previous and next characters are the same 
                     count = count + 1 # we increase count variable
                     j += 1 # we increase j position
                 else:
                     break
                 
             encoded_message = encoded_message + str(count) + ch # "" + count converted to string + character (ch)
             i = j + 1
         return encoded_message
    
    
    text = 'Hello World'
    encoded_message = encode(text)
    print('The encoded message is the output ', encoded_message)
    

    (Please note: I do not know the RLE algorithm but just looked at your code.)