본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 40

반응형

Q>

You are given a string where you have to find its first word.

(당신은 첫 단어를 찾아야 만하는 문자열을 받게됩니다.)

When solving a task pay attention to the following points:

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

There can be dots and commas in a string.

(문자열에는 점과 쉼표가있을 수 있습니다.)

A string can start with a letter or, for example, a dot or space.

(문자열은 문자 또는 도트 또는 공백으로 시작할 수 있습니다.)

A word can contain an apostrophe and it's a part of a word.

(단어에는 아포스트로피가 포함될 수 있으며 단어의 일부입니다.)

The whole text can be represented with one word and that's it.

(전체 텍스트는 한 단어로 표현할 수 있습니다.)


Input: A string.

(입력 : 문자열.)


Output: A string.

(출력 : 문자열.)


Example:


1 first_word("Hello world") == "Hello"

2 first_word("greetings, friends") == "greetings"


How it is used: the first word is a command in a command line

(사용 방법 : 첫 번째 단어는 명령 행의 명령입니다.)

Precondition: the text can contain a-z A-Z , . '

(사전 조건 : 본문은 a-z A-Z를 포함 할 수 있습니다. ')


A>

def first_word(text: str) -> str:
"""
returns the first word in a given text.
"""
# your code here
# replace : 문자열 변경 메소드(. -> 공백 / , -> 공백)
# strip : 문자열 제거 메소드(공백 삭제)
text = text.replace('.', ' ').replace(',', ' ').strip()
# split : 문자열 나누기
text = text.split()

return text[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!")


O>

Example:

Hello

Coding complete? Click 'Check' to earn cool rewards!


Process finished with exit code 0


S>


반응형

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