How does the variable p work in this code?

3

I do not know how the variable p of the loop while is working on this code.

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

Would not this cause conflict? I say this because the same variable name that is being used to iterate is the same name that is being used to store s.find( "tigre", p ) in the first line of while .

How are these p 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
    
asked by anonymous 05.05.2017 / 21:28

3 answers

3

Regardless of the language, the '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 
    
05.05.2017 / 21:44
7

The second parameter of the find function sets the offset against the text. That is, s.find("tigre", 0) will search for the first occurrence of the word "tiger" from position 0 of s , already s.find("tigre", 10) will search for the first occurrence of the word from position 10 of s . What the program does is update the value of this offset 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 is 0, so:

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 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, run loop . The new value of p will be the return of the position where the word tigre is found in the text, with a p offset. Since p is zero, the function will search from position 0 of the text, locating the word in 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 displaying on 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 find function will look for the word in the text from position p , which is 4, so it will no longer consider the first occurrence of the word. The new value of p , in this case, will 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

At the next iteration, the find function will try to locate the word tigre in the original text, but only from position 16, ignoring the first two occurrences. This way, 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                                                                   

Once displayed, 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 find function will search for the word tigre from position 29. Since there are no more occurrences of the word, -1 is returned, so p will be -1, the while condition is undone and the program terminates.

    
05.05.2017 / 22:00
3

See the commented code below:

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

    
05.05.2017 / 21:41