728x90
1. Problem: https://py.checkio.org/en/mission/words-order/
2. My solutions
def words_order(text: str, words: list) -> bool:
# your code here
original_list = text.split()
# check that words is in text
for word in words:
if word not in original_list: return False
if words.count(word) != 1: return False
order = list(map(lambda x: original_list.index(x), words))
if order == sorted(order): return True
else: return False
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
assert words_order("hi world im here", ["country", "world"]) == False
assert words_order("hi world im here", ["wo", "rld"]) == False
assert words_order("", ["world", "here"]) == False
print("Coding complete? Click 'Check' to earn cool rewards!")
3. Best solution
def words_order(text: str, words: list) -> bool:
return list(dict.fromkeys([word for word in text.split() if word in words])) == words
728x90
'프로그래밍 문제 > [Python] CheckIO' 카테고리의 다른 글
[CheckIO] Convert To Title case (0) | 2024.02.18 |
---|---|
[CheckIO] Count Vowels (0) | 2024.02.18 |
[Check-IO] Conversion into CamelCase (0) | 2021.12.25 |
[Check-IO] Conversion from CamelCase (0) | 2021.12.20 |
[Check-IO] The Most Wanted Letter (0) | 2021.12.19 |
댓글