Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Keywords Finder by Phil15
import re
def checkio(text, words):
# All spans for each word.
spans = sorted(m.span(1) for word in map(re.escape, words.split())
for m in re.finditer(fr'(?=({word}))', text, flags=re.I))
# Merge intersecting spans.
L = []
for start, end in spans:
if not L or start >= L[-1][1]:
L.append([start, end])
elif L[-1][1] < end:
L[-1][1] = end
L.append((None,)*2)
# List text blocks and tags. Then join them to get output text.
res = [text[:L[0][0]]]
for (start, end), (next_start, _) in zip(L, L[1:]):
res += ['', text[start: end], '', text[end: next_start]]
return ''.join(res)
Oct. 1, 2019
Comments: