본문 바로가기

Python_Matter/[Check_IO]Elementary

Correct Sentence

반응형

Quiz>

For the input of your function, you will be given one sentence. 

You have to return a corrected version, that starts with a capital letter and ends with a period (dot).

 

Pay attention to the fact that not all of the fixes are necessary. 

If a sentence already ends with a period (dot), then adding another one will be a mistake.

 

Input:

A string.

 

Output:

A string.

 

Example:

correct_sentence("greetings, friends") == "Greetings, friends."
correct_sentence("Greetings, friends") == "Greetings, friends."
correct_sentence("Greetings, friends.") == "Greetings, friends."

 

Precondition:

No leading and trailing spaces, text contains only spaces, a-z A-Z , and .

 

def correct_sentence(text: str) -> str:
    """
        returns a corrected sentence which starts with a capital letter
        and ends with a dot.
    """
    # your code here
    return text


if __name__ == '__main__':
    print("Example:")
    print(correct_sentence("greetings, friends"))
    
    # These "asserts" are used for self-checking and not for an auto-testing
    assert correct_sentence("greetings, friends") == "Greetings, friends."
    assert correct_sentence("Greetings, friends") == "Greetings, friends."
    assert correct_sentence("Greetings, friends.") == "Greetings, friends."
    assert correct_sentence("hi") == "Hi."
    assert correct_sentence("welcome to New York") == "Welcome to New York."
    
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

Solve>

1. 입력값에서 첫번째 글자를 대문자로 변환하고 뒤에 입력값 나머지 부분을 붙인다.

   upper를 사용하면 소문자를 대문자로 변환 할 수 있다.

def correct_sentence(text: str):
    text = text[0].upper() + text[1:]

 

2. 마지막에 있는 글자에 .가 없으면 입력값에 .을 붙인다

   endswith를 사용하면 마지막에 있는 글자를 찾을 수 있다.

def correct_sentence(text: str):
    if not text.endswith('.'):
        text += '.'

 

Code>

def correct_sentence(text: str):
    text = text[0].upper() + text[1:]

    if not text.endswith('.'):
        text += '.'

    return text

 

Example>

if __name__ == '__main__':
    print("Example:")
    print(correct_sentence("greetings, friends"))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert correct_sentence("greetings, friends") == "Greetings, friends."
    assert correct_sentence("Greetings, friends") == "Greetings, friends."
    assert correct_sentence("Greetings, friends.") == "Greetings, friends."
    assert correct_sentence("hi") == "Hi."
    assert correct_sentence("welcome to New York") == "Welcome to New York."

    print("Coding complete? Click 'Check' to earn cool rewards!")

 

Result>

Example:

Greetings, friends.

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

반응형

'Python_Matter > [Check_IO]Elementary' 카테고리의 다른 글

Acceptable Password I  (0) 2020.04.11
All Upper I  (1) 2020.04.11
Fizz Buzz  (0) 2020.04.11
Say Hi  (0) 2020.04.11
First Word (simplified)  (0) 2020.04.10