How does variable p work in this code?

Asked

Viewed 85 times

3

I don’t know how the variable p of loop while is working on that code.

If the variable p is the iteration variable, because the exercise also uses a variable p within the while to store the s.find( "tigre", p )?

Wouldn’t that cause conflict? I say this because the same variable name that is being used to make the iteration, is the same name that is being used to store the s.find( "tigre", p ) in the first line of while.

Like these p are working?

s = "um tigre, dois tigres, três tigres"

p=0
while (p > -1):
    p = s.find("tigre", p)
    if p >= 0:
        print("Posição: %d" % p)
        p += 1

3 answers

7

The second function parameter find sets the offset relative to the text. This is, s.find("tigre", 0) will seek the first occurrence of the word "tiger" from position 0 of s, already s.find("tigre", 10) seek the first occurrence of the word from position 10 of s. What the program does is update the value of this shift so that the same occurrence of the word is not considered more than once.

TL;DR

Our text:

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m |   | t | i | g | r | e | , |   | d | o | i | s |   | t | i | g | r | e | s | , |   | t | r | ê | s |   | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  0   1   2   3   ...

The value of p initial is 0, therefore:

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m |   | t | i | g | r | e | , |   | d | o | i | s |   | t | i | g | r | e | s | , |   | t | r | ê | s |   | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  0   1   2   3   ...
  ^
  p

While p is greater than -1, execute the loop. The new value of p will be the return of the position where the word is found tigre in the text, with an offset of p. Like p is zero, the function will search from position 0 of the text, locating the word at position 3. Then, the new value of p will be 3.

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m |   | t | i | g | r | e | , |   | d | o | i | s |   | t | i | g | r | e | s | , |   | t | r | ê | s |   | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  0   1   2   3   ...
              ^
              p

After displayed on the screen, p is incremented by 1:

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m |   | t | i | g | r | e | , |   | d | o | i | s |   | t | i | g | r | e | s | , |   | t | r | ê | s |   | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  0   1   2   3   4  ...
                  ^
                  p

In the next iteration, the function find will search for the word in the text from the position p, which is worth 4, so he will not consider more the first occurrence of the word. The new value of p, in this case, it shall be 15.

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m |   | t | i | g | r | e | , |   | d | o | i | s |   | t | i | g | r | e | s | , |   | t | r | ê | s |   | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  ...
                                                              ^
                                                              p

Again, when displayed on the screen, the value of p is incremented by 1.

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m |   | t | i | g | r | e | , |   | d | o | i | s |   | t | i | g | r | e | s | , |   | t | r | ê | s |   | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  ...
                                                                  ^
                                                                  p

In the next iteration, the function find will attempt to locate the word tigre in the original text, but only from position 16, disregarding the first two occurrences. Thus, the new value of p will be 28.

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m |   | t | i | g | r | e | , |   | d | o | i | s |   | t | i | g | r | e | s | , |   | t | r | ê | s |   | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33
                                                                                                                  ^
                                                                                                                  p                                                                   

After displayed on screen, again the value of p is incremented by 1:

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m |   | t | i | g | r | e | , |   | d | o | i | s |   | t | i | g | r | e | s | , |   | t | r | ê | s |   | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33
                                                                                                                      ^ 
                                                                                                                      p                                          

Finally, in the last iteration, the function find will seek by word tigre from position 29. Since there are no more occurrences of the word, it is returned -1, therefore p will be worth -1, causing the condition of the while is undone and the program is terminated.

  • 3

    Bah, very good.

  • what a mood! congratulations =]

  • Dude, you MYTHED on the answer, meme Thug life for you, hahaha

3

See below the commented code:

s = "um tigre, dois tigres, três tigres"

p=0 #inicializa a variável com 0
while (p > -1): #enquanto o p for maior do que -1
   #Se encontrar tigre na string retorna a posição
   #Se não encontrar retorna -1
   p = s.find("tigre", p) 
   if p >= 0: #se p maior ou igual a zero encontrou na string
      print("Posição: %d" % p) # imprime a posição
      p += 1

See more about the find function

3


Regardless of language, p will be the index of the word tiger, if there are no more occurrences of the word, it will exit the loop.

s = "um tigre, dois tigres, três tigres"

p=0 #p inicia com 0 pra entrar no laço
while (p > -1):
    p = s.find("tigre", p) #busca o índice de tigre, a partir da posição p
    if p >= 0: #se encontrou, imprime o índice
        print("Posição: %d" % p)
        p += 1 #incrementa o índice da última ocorrência encontrada para não repetir a palavra 
  • What I didn’t understand was why was defined a variable of name p below the line of while, that is, (p = ) that has the same name of the iteration variable, that is, should not give error? ,why if the variable (p) is already the name of the iteration variable, how does the program accept the same variable (p) to receive s.find( "tiger", p ) ? where p inside find() represents the iteration variable?

  • it is receiving the value returned by the find function, inside the while if there is no change of p at any time, it would be in an infinite loop

Browser other questions tagged

You are not signed in. Login or sign up in order to post.