본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 51

반응형

Q>

Let's teach the Robots to distinguish words and numbers.

(로봇에게 단어와 숫자를 구별하도록 가르쳐 봅시다.)

You are given a string with words and numbers separated by whitespaces (one space).

(단어와 숫자가 공백으로 구분 된 문자열 (한 칸)이 주어집니다.)

The words contains only letters.

(단어에는 글자 만 들어 있습니다.)

You should check if the string contains three words in succession.

(문자열에 연속해서 세 단어가 포함되어 있는지 확인해야합니다.)

For example, the string "start 5 one two three 7 end" contains three words in succession.

(예를 들어, 문자열 "start 5 one two two three 7 end"에는 세 단어가 연속적으로 들어 있습니다.)

 

Input: A string with words.(단어가있는 문자열.)

 

Output: The answer as a boolean.(대답은 부울입니다.)

 

Example:

checkio("Hello World hello") == True
checkio("He is 123 man") == False
checkio("1 2 3 4") == False
checkio("bla bla bla bla") == True
checkio("Hi") == False

 

How it is used: This teaches you how to work with strings and introduces some useful functions.

                     (이것은 문자열로 작업하는 방법을 가르치고 몇 가지 유용한 기능을 소개합니다.)

 

Precondition: The input contains words and/or numbers.

                   (입력에 단어 및 / 또는 숫자가 포함됩니다.)

                   There are no mixed words (letters and digits combined).

                   (혼합 된 단어 (문자와 숫자를 합친 단어)는 없습니다.)
                   0 < len(words) < 100

 

A>

def checkio(words: str) -> bool:
    # 초기값 지정
    count = 0

    # split : 문자열 나누기
    for word in words.split():
        # isalpha : 문자열이 알바벳으로 구성 되어있는지 확인하는 메소드
        # 문자로 구성 되어있으면 count 1
        if word.isalpha():
            count += 1
        # 그 이외는 0
        else:
            count = 0
        # 카운터가 3이 넘으면 True
        if count >= 3:
            return True
    return False

# These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    print('Example:')
    print(checkio("Hello World hello"))

    assert checkio("Hello World hello") == True, "Hello"
    assert checkio("He is 123 man") == False, "123 man"
    assert checkio("1 2 3 4") == False, "Digits"
    assert checkio("bla bla bla bla") == True, "Bla Bla"
    assert checkio("Hi") == False, "Hi"
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

 

O>

Example:
True
Coding complete? Click 'Check' to review your tests and earn cool rewards!

Process finished with exit code 0

 

S>

https://py.checkio.org

반응형

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