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

[CheckIO] Popular words

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

Problems: py.checkio.org/en/mission/popular-words/

My solution

def popular_words(text: str, words: list) -> dict:
    # your code here
    
    counts = []
    for f_word in words:
        counting_numbers = 0
        for match_words in (text.lower()).split():
            if match_words == f_word: counting_numbers = counting_numbers + 1
        counts.append(counting_numbers)
    
    return dict(zip(words, counts))


if __name__ == '__main__':
    print("Example:")
    print(popular_words('''
When I was One
I had just begun
When I was Two
I was nearly new
''', ['i', 'was', 'three', 'near']))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert popular_words('''
When I was One
I had just begun
When I was Two
I was nearly new
''', ['i', 'was', 'three', 'near']) == {
        'i': 4,
        'was': 3,
        'three': 0,
        'near': 0
    }
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

A good solution that I see

def popular_words(text, words):
    lower_count = text.lower().split().count
    return {word: lower_count(word) for word in words}

 

728x90

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

[CheckIO] sort array by element frequency  (0) 2020.12.30
[CheckIO] Second Index  (0) 2020.12.24
[CheckIO] non unique elements  (0) 2020.12.22
CheckIO - Between Markers  (0) 2020.12.21
CheckIO - Bigger price  (0) 2020.12.20

댓글