Advertisement
Guest User

Untitled

a guest
May 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. def get_shift(substr):
  2.     shift = {}
  3.     for i in range(0,len(substr)-1):
  4.         shift[substr[i]] = len(substr)-i-1
  5.     if substr[len(substr)-1] not in shift:
  6.         shift[substr[len(substr)-1]] = len(substr)
  7.     return shift
  8.  
  9. def horspul(str, substr):
  10.     shift = get_shift(substr)
  11.     i = len(substr) -1
  12.     while i < len(str):
  13.         for j in range(0, len(substr)):
  14.             if str[i-j] != substr[len(substr)-1-j]:
  15.                 if str[i-j] in shift:
  16.                     i = i+shift[str[i-j]]
  17.                     break
  18.                 else:
  19.                     i+= len(substr)
  20.                     break
  21.  
  22.             if j >= len(substr) - 1:
  23.                 return (i - len(substr) +1)
  24.  
  25.     return -1
  26.  
  27. print(get_shift('ad'))
  28. print(horspul('adad', 'ad'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement