davide1409

Find

Nov 17th, 2019
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. def Find(s,sub_string,start = 0, stop = None):
  2. #@param s: string; stringa su cui cercare
  3. #@param sub_string: string; stringa da cercare
  4. #@param start: int
  5. #@param stop: int
  6.    
  7.     if stop == None or stop >= len(s):
  8.         stop = len(s)
  9.  
  10.     if start>=len(s) and stop >=len(s):
  11.         return -1
  12.  
  13.     lensub = len(sub_string)
  14.    
  15.     if(stop-start)+1>=lensub:
  16.         if lensub == 1:
  17.             for i in range(start,stop):
  18.                 if s[i] == sub_string:
  19.                     return i
  20.                
  21.             return -1
  22.  
  23.         else:
  24.             for i in range(start,stop):
  25.                 if(i+lensub)-1 > stop:
  26.                     return -1
  27.                 else:
  28.                     if s[i:lensub+i] == sub_string:
  29.                         return i
  30.  
  31.             return -1
  32.  
  33.     else:
  34.         return -1
Add Comment
Please, Sign In to add comment