Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Keywords Finder by Kurush
# migrated from python 2.7
import re
def checkio(text, words):
keywords = words.split()
if len(keywords) == 0: return text
res = True
while res == True:
keywords, res = add_joins(keywords)
for char in list(r"\.^$*+?()[{|"):
keywords = [item.replace(char, "\\" + char) for item in keywords]
text = re.sub("(?i)" + "(" + "|".join(sorted(keywords, key = len, reverse = True)) + ")", r"\1", text)
text = re.sub(r"(?i)(.*?)\1" , r"\1\1", text)
return text
def add_joins(keywords):
new_keywords = set()
for word1 in keywords:
new_keywords.add(word1)
for word2 in keywords:
if word1 != word2:
new_word = join_words(word1, word2)
if new_word != "": new_keywords.add(new_word)
return list(new_keywords), len(new_keywords) > len(keywords)
def join_words(a, b):
index = 1
while True:
if index > len(a) or index > len(b): return ""
if a[-index:] == b[:index]:
return a + b[index:]
index += 1
return ""
#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"
assert (checkio("JabberWocky", "jabber erwo ock") ==
"JabberWocky"), "Several intersections"
assert (checkio("aaaa", "aa a") ==
"aaaa"), "aaaa"
June 28, 2014