Advertisement
Serdalis

Python - Find permutation

Nov 1st, 2011
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. perms = ["ab", "cd", "ef"]
  2. string ="asdansdkjajskdnklabklmkalabmkklmabcdkmlabcdefkmlkm"
  3.  
  4.  
  5. def findPerm(li):
  6.     lowerFound = 0
  7.     upperFound = 0
  8.     foundItems = []
  9.     # loop through the whole string - length of the first item
  10.     for i in range(0,len(string)-len(li[0])):
  11.         # if the first item is found, begin searching for the rest
  12.         if(string[i:i+len(li[0])] == li[0]):
  13.             foundItems = [0]
  14.             lowerFound = i
  15.             upperFound = i+len(li[0])
  16.             found = 0
  17.             # try to find all elements of the permutation
  18.             while found < len(foundItems):
  19.                 found = len(foundItems)
  20.                 # search for 1 element of the permutation either before or after the found permutation
  21.                 for i in range(0,len(li)):
  22.                     if(i not in foundItems):
  23.                         if(string[lowerFound-len(li[i]):lowerFound] == li[i]):
  24.                             lowerFound -= len(li[i])
  25.                             foundItems.append(i)
  26.                             break
  27.                            
  28.                         if(string[upperFound:upperFound+len(li[i])] == li[i]):
  29.                             upperFound += len(li[i])
  30.                             foundItems.append(i)
  31.                             break
  32.             if(len(foundItems) == len(li)):
  33.                 return lowerFound, upperFound
  34.     return 0,0
  35.        
  36.  
  37. print(findPerm(perms))
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement