In [38]:
def answer(document, searchTerms):
"""returns the shortest substring of document that includes all the search terms
document - a string containing only lowercase letters, between 1 and 500 characters,
searchTerms - will be a list of lowercase strings"""
wordlist = document.split()
doc_length = len(wordlist)
candidates = []
short_substr = (0, doc_length)
for i in range(doc_length):
if wordlist[i] in searchTerms:
new_candidate = (i, list(searchTerms))
candidates.append(new_candidate)
for candidate in candidates:
if wordlist[i] in candidate[1]:
candidate[1].remove(wordlist[i])
if not candidate[1]:
substr_len = i - candidate[0]
if substr_len < short_substr[1] - short_substr[0]:
short_substr = (candidate[0], i)
return ' '.join(wordlist[short_substr[0]:short_substr[1]+1])
In [39]:
document1 = "many google employees can program"
searchTerms1 = ["google", "program"]
print(answer(document1, searchTerms1))
In [40]:
document2 = "a b c d a"
searchTerms2 = ["a", "c", "d"]
print(answer(document2, searchTerms2))
In [18]:
emp = [1]
if emp:
print('hi')