Skip to content

Instantly share code, notes, and snippets.

@rakeshsukla53
Created August 31, 2015 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rakeshsukla53/e4a149db75e7772408b1 to your computer and use it in GitHub Desktop.
Save rakeshsukla53/e4a149db75e7772408b1 to your computer and use it in GitHub Desktop.
__author__ = 'rakesh'
def answer(document, searchTerms):
loopSearchTerms = []
for j in searchTerms:
loopSearchTerms.append(j)
stringList = document.split(" ")
start, end = 0, 0
shortSnippet = []
count = 1
for k in range(len(stringList)):
start = k
for i in range(k, len(stringList)):
if stringList[i] in loopSearchTerms:
loopSearchTerms.remove(stringList[i])
end = i
if len(loopSearchTerms) == 0:
if count == 1:
shortSnippet.append(stringList[start:end+1])
count += 1
if len(stringList[start:end+1]) < len(shortSnippet[0]):
shortSnippet = []
shortSnippet.append(stringList[start:end+1])
for j in searchTerms:
loopSearchTerms.append(j)
break
try:
return " ".join(shortSnippet[0])
except IndexError:
pass
print answer("a b d a c a c c d a", ["a", "c", "d"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment