SHARE
TWEET

Untitled

a guest Nov 19th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Código em python para busca de padrões
  2. #Otimizado para pesquisa onde os caracteres são diferentes
  3. def listaindice(t):
  4.     cont = 0
  5.     for i in t:
  6.         t += " %s[%i]" %(i, cont)
  7.         cont += 1
  8.     return t
  9.  
  10. def search(padrao, texto):
  11.     M = len(padrao)
  12.     N = len(texto)
  13.     i = 0
  14.     while i <= N-M:
  15.         for j in range(M):
  16.             if texto[i+j] != padrao[j]:
  17.                 break              
  18.             j = j + 1
  19.  
  20.         if j==M: # Quando o padrão for encontrado volta ao loop
  21.             print("Padrao encontrado no indice:", i)
  22.             i = i + M
  23.         elif j==0:
  24.             i = i + 1
  25.         else:
  26.             i = i + j
  27.  
  28. texto = "abacate"
  29. padrao = "aba"
  30.  
  31. print(listaindice(texto))
  32. search(padrao, texto)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top