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

CheckIO - first word

by UltraLowTemp-Physics 2020. 12. 11.
728x90

Problem: py.checkio.org/en/mission/first-word/

My Solution; 

import re
def first_word(text: str) -> str:
    """
        returns the first word in a given text.
    """
    # your code here

    words = re.sub('\.', " ", text)
    words = re.sub(",", " ", words)
            
    return words.split()[0]


if __name__ == '__main__':
    print("Example:")
    print(first_word("Hello world"))
    
    # These "asserts" are used for self-checking and not for an auto-testing
    assert first_word("Hello world") == "Hello"
    assert first_word(" a word ") == "a"
    assert first_word("don't touch it") == "don't"
    assert first_word("greetings, friends") == "greetings"
    assert first_word("... and so on ...") == "and"
    assert first_word("hi") == "hi"
    assert first_word("Hello.World") == "Hello"
    print("Coding complete? Click 'Check' to earn cool rewards!")

A good solution that I see

import re

def first_word(text: str) -> str:
    return re.search("([\w']+)", text).group(1)
728x90

'프로그래밍 문제 > [Python] CheckIO' 카테고리의 다른 글

CheckIO - Count digits  (0) 2020.12.14
CheckIO - Days between  (0) 2020.12.12
CheckIO - three words  (0) 2020.12.11
[CheckIO] Right to Left  (0) 2020.12.09
CheckIO - Even the last  (0) 2020.12.08

댓글