fork download
  1. def calculateScore(text, prefixString, suffixString):
  2. result = {}
  3. lenText = len(text)
  4.  
  5. while lenText > 0:
  6. for i in range(len(text) + 1 - lenText):
  7. substring = text[i:i + lenText]
  8.  
  9. # calc the pre_text_pattern_score
  10. pre_text_pattern_score = min(len(prefixString), len(substring))
  11.  
  12. while pre_text_pattern_score > 0 and substring[:pre_text_pattern_score] != prefixString[-pre_text_pattern_score:]:
  13. pre_text_pattern_score -= 1
  14.  
  15. # calc the post_text_pattern_score
  16. post_text_pattern_score = min(len(suffixString), len(substring))
  17.  
  18. while post_text_pattern_score > 0 and substring[-post_text_pattern_score:] != suffixString[:post_text_pattern_score]:
  19. post_text_pattern_score-= 1
  20.  
  21. # calculate the pattern_score
  22. pattern_score = pre_text_pattern_score + post_text_pattern_score
  23.  
  24. if not pattern_score in result:
  25. # resets the dictionary key
  26. result[pattern_score] = []
  27.  
  28. result[pattern_score].append(substring)
  29.  
  30. lenText -= 1 # reduce lenText by 1
  31.  
  32. # store the highest key, so we can sort the right item to return
  33. maximum_pattern_score = max(result.keys())
  34.  
  35. # make sure to sort the lexicographically lowest string of the highest key
  36. result[maximum_pattern_score].sort()
  37.  
  38. # return the lexicographically highest key
  39. return result[maximum_pattern_score][0]
Success #stdin #stdout 0.1s 14044KB
stdin
engine
raven
ginkgo
stdout
Standard output is empty