본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 43

Q>

In this mission your task is to determine the popularity of certain words in the text.

(이 임무에서 당신의 임무는 텍스트의 특정 단어의 인기를 결정하는 것입니다.)

At the input of your function are given 2 arguments: the text and the array of words the popularity of which you need to determine.

(함수의 입력에는 두 가지 인수가 제공됩니다. 텍스트와 단어의 배열을 결정해야합니다.)

When solving this task pay attention to the following points:

(이 작업을 해결할 때 다음 사항에주의하십시오.)

  • The words should be sought in all registers. This means that if you need to find a word "one" then words like "one", "One", "oNe", "ONE" etc. will do.(
  • The search words are always indicated in the lowercase.
  • If the word wasn’t found even once, it has to be returned in the dictionary with 0 (zero) value.

 

Input: The text and the search words array.

         (텍스트 및 검색 단어 배열입니다)

 

Output: The dictionary where the search words are the keys and values are the number of times when those words are occurring in a given text.

(검색 단어가 키이고 값은 해당 단어가 주어진 텍스트에서 나타나는 횟수입니다.)

 

Example:

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
}

 

Precondition:
The input text will consists of English letters in uppercase and lowercase and whitespaces.

(입력 텍스트는 영문 대문자와 소문자 및 공백으로 구성됩니다.)

 

A>

def popular_words(text: str, words: list) -> dict:
    # your code here
    # 대소문자 상관없이 찾는다.
    # lower : 소문자로 변경 메소드
    text = text.lower()
    # split : 공백단위로 나누기
    splitted_words = text.split()
    # 돌려줄 딕셔너리
    answer = {}

    for word in words:
        # count : 부분 문자열 세는 메소드
        answer[word] = splitted_words.count(word)

    return answer

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!")

 

O>

Example:
{'i': 4, 'was': 3, 'three': 0, 'near': 0}
Coding complete? Click 'Check' to earn cool rewards!

Process finished with exit code 0

 

S>

https://py.checkio.org

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 45  (0) 2019.06.10
Python Learn the basics Quiz 44  (0) 2019.06.10
Python Learn the basics Quiz 43  (0) 2019.06.09
Python Learn the basics Quiz 42  (0) 2019.06.09
Python Learn the basics Quiz 41  (0) 2019.06.09
Python Learn the basics Quiz 40  (0) 2019.06.08