Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Keywords Finder by Tinus_Trotyl
def checkio(text, words):
txt, words, keys = text.lower(), sorted(words.lower().split()), []
for word in words:
i = 0
while word in txt[i:]:
i += txt[i:].index(word)
keys.append((i, i + len(word) - 1))
i += 1
if not keys: return text
keys = sorted(keys)
spans = [keys[0]]
for key in keys[1:]:
if spans[~0][1] >= key[0]: spans[~0] = (spans[~0][0], max(spans[~0][1], key[1]))
else: spans.append(key)
spanned, last = [], 0
for span in spans:
spanned.append(text[last:span[0]] + '' + text[span[0]:span[1] + 1] + '')
last = span[1] + 1
spanned.append(text[last:])
return ''.join(spanned)
Dec. 10, 2019
Comments: