1

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

1 Answers1

0

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.)

rosa b.
  • 1,759
  • 2
  • 15
  • 18
  • Ok that's more clear now but for the i = j + 1 is for the i increased accordingly at the end of the inner loop right ? – MrTiredVeryTired May 07 '22 at 01:18
  • Yep, that’s correct. – rosa b. May 07 '22 at 06:19
  • But we could've write i += 1 instead no ? – MrTiredVeryTired May 07 '22 at 08:01
  • Using i+=1 would be ok as long as the input word does not contain any identical consecutive letters. But then again the inner loop is not required at all... as it is its only task to summarise these identical consecutive letters. Just use ` aaa` as input and check what happens when using i=j+1 vs. i+=1. – rosa b. May 07 '22 at 08:40
  • Okay, thanks for your explanation, I just have two last questions If you don't mind ? – MrTiredVeryTired May 09 '22 at 02:34
  • What is actually the real meaning behind i = j + 1 it's like i increasing when the inter loop increase of 1 element instead of the outer loop increasing of 1 (i = i + 1) And last thing how the character for example with 'aaa' why in the result we got 3a instead of 1a2a3a ? – MrTiredVeryTired May 09 '22 at 02:49
  • 1
    Note sure I understand the questions correctly. But I will give it a try: (1/2) The RLE algorithm compresses the input data by replacing every sequence of identiac characters by the sequence length and the character itself. So `aaa` becomes `3a`, `aabccc` becomes `2a1b3c` etc. Using `i = j + 1` makes sure the algorithm 'hops' over the characters that have been evalutated already. – rosa b. May 11 '22 at 08:34
  • 1
    (2/2) Take the string `aabccc` for example. Starting with `i=0` and `ch=a`. The inner loop checks if there is more than one `a`. And yes, indeed: There are 2 `a`s. So `aa` in the original string is replaced with `2a`. Now the first two characters of the original string have been handled, off you go to the 3rd letter, which is a `b`in the example. And so on... – rosa b. May 11 '22 at 08:34