Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Keywords Finder by sawako.oono
import re
def checkio(text, words):
words=words.split(" ")
words=[x for x in words if all((x!=" ",x!=""))]
for i in range(len(words)):
for l in ("\\","?","!"):
words[i]=words[i].replace(l,"\\"+l)
span=[]
for word in words:
regex1=re.compile(r"((?={}))".format(word),re.IGNORECASE)
regex2=re.compile(r"((?<={}))".format(word),re.IGNORECASE)
res1=regex1.finditer(text)
res2=regex2.finditer(text)
start=[]
for r in res1:
start.append(r.start())
end=[]
for r in res2:
end.append(r.start())
for i in range(len(start)):
span.append((start[i],end[i]))
span.sort()
for i in range(len(span)-1):
if span[i+1][0]=span[i+1][0]:
span.pop(i)
for s in sorted(span,reverse=True):
text=text[:s[1]]+""+text[s[1]:]
text=text[:s[0]]+""+text[s[0]:]
return text
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert (checkio("This is only a text example for task example.", "example") ==
"This is only a text example for task example."), "Simple test"
assert (checkio("Python is a widely used high-level programming language.", "pyThoN") ==
"Python is a widely used high-level programming language."), "Ignore letters cases, but keep original"
assert (checkio("It is experiment for control groups with similar distributions.", "is im") ==
"It is experiment for control groups with similar distributions."), "Several subwords"
assert (checkio("The National Aeronautics and Space Administration (NASA).", "nasa THE") ==
"The National Aeronautics and Space Administration (NASA)."), "two spaces"
assert (checkio("Did you find anything?", "word space tree") ==
"Did you find anything?"), "No comments"
assert (checkio("Hello World! Or LOL", "hell world or lo") ==
"Hello World! Or LOL"), "Contain or intersect"
June 23, 2021
Comments: