Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Keywords Finder by yoichi
def getLoweredKeys(words):
return words.lower().split()
def findKeys(text, keys):
found = []
lowertext = text.lower()
for key in keys:
start = 0
end = len(text)
while start != end:
index = lowertext[start:].find(key)
if index < 0:
break
found.append((start + index, True))
found.append((start + index + len(key), False))
start += index + 1
return sorted(found)
def checkio(text, words):
found = findKeys(text, getLoweredKeys(words))
start = 0
status = 0
result = ""
for (index, isStart) in found:
if isStart:
if status == 0:
result += text[start:index]
start = index
result += ""
status += 1
else:
status -= 1
if status == 0:
result += text[start:index]
start = index
result += ""
result += text[start:]
return result
#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"
Nov. 11, 2014