본문 바로가기
프로그래밍 문제/[Python] CheckIO

[Check IO] words-order

by UltraLowTemp-Physics 2022. 2. 27.
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

댓글