-
-
Save rakeshsukla53/e4a149db75e7772408b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__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