Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. """
  2. You have two arrays of strings, words and parts. Return an array that contains the strings from words,
  3. modified so that any occurrences of the substrings from parts are surrounded by square brackets [],
  4. following these guidelines:
  5.  
  6. If several parts substrings occur in one string in words, choose the longest one. If there is still
  7. more than one such part, then choose the one that appears first in the string.
  8.  
  9. For words = ["Apple", "Melon", "Orange", "Watermelon"] and parts = ["a", "mel", "lon", "el", "An"],
  10. the output should be:
  11.  
  12. findSubstrings(words, parts) = ["Apple", "Me[lon]", "Or[a]nge", "Water[mel]on"].
  13.  
  14. """
  15.  
  16. def failure(pattern):
  17. matchCount = 0
  18. d = set()
  19. indices = [0] * len(pattern)
  20. for i in range(len(pattern)):
  21. if pattern[i] in d:
  22. matchCount += 1
  23. else:
  24. matchCount = 0
  25. d.add(pattern[i])
  26. indices[i] = matchCount
  27. return indices
  28.  
  29.  
  30. def KMP(text, pattern):
  31. failureIndices = failure(pattern)
  32. i = j = 0
  33. while i < len(text):
  34. if text[i] == pattern[j]: # character match
  35. if j == len(pattern)-1:
  36. return i-len(pattern)+1 # complete match
  37. i += 1
  38. j += 1
  39. elif j > 0: # no match but we have advanced through pattern
  40. j = failureIndices[j-1]
  41. else: # no match and have not advanced through pattern
  42. i += 1
  43. return -1 # no match exists
  44.  
  45.  
  46. def findSubstrings(words, parts):
  47. for i in range(len(words)):
  48. length = -1
  49. partLocation = -1
  50. wordIndex = -1
  51. for j in range(len(parts)):
  52. index = KMP(words[i], parts[j])
  53. if index is not -1 and len(parts[j]) > length:
  54. length = len(parts[j])
  55. partLocation = j
  56. wordIndex = index
  57. # elif is for finding first occurrence when we have equal length answers
  58. elif index is not -1 and len(parts[j]) == length:
  59. if index < wordIndex:
  60. length = len(parts[j])
  61. partLocation = j
  62. wordIndex = index
  63. if partLocation is not -1:
  64. words[i] = words[i][:wordIndex] + "[" + words[i][wordIndex:wordIndex+length] + "]" + words[i][wordIndex+length:]
  65. return words
  66.  
  67.  
  68. words = ["neuroses", "myopic", "sufficient", "televise", "coccidiosis", "gules", "during", "construe", "establish", "ethyl"]
  69. parts = ["aaaaa", "Aaaa", "E", "z", "Zzzzz", "a", "mel", "lon", "el", "An", "ise", "d", "g","wnoVV", "i", "IUMc", "P", "KQ", "QfRz", "Xyj", "yiHS"]
  70. print(findSubstrings(words, parts))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement