Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
words order solution in Clear category for Words Order by hbczmxy
def words_order(text: str, words: list) -> bool:
# your code here
text_list = text.split()
start = 0
for word in words:
# Check for complete English words
if word not in text_list:
return False
# Check for the same English words
if word in words[words.index(word)+1:]:
return False
# Check for word's order
index = text.find(word, start)
start = index + len(word)
if index == -1:
return False
else:
return True
if __name__ == '__main__':
print("Example:")
print(words_order('hi world im here', ['world', 'here']))
# These "asserts" are used for self-checking and not for an auto-testing
assert words_order('hi world im here', ['world', 'here']) == True
assert words_order('hi world im here', ['here', 'world']) == False
assert words_order('hi world im here', ['world']) == True
assert words_order('hi world im here', ['world', 'here', 'hi']) == False
assert words_order('hi world im here', ['world', 'im', 'here']) == True
assert words_order('hi world im here', ['world', 'hi', 'here']) == False
assert words_order('hi world im here', ['world', 'world']) == False, "two same words"
assert words_order('hi world im here', ['country', 'world']) == False
assert words_order('hi world im here', ['wo', 'rld']) == False, "incomplete word"
assert words_order("hi world world im here",["world","world"]) == False, "two same words"
assert words_order('', ['world', 'here']) == False
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 9, 2021