취미생활/CheckIO

[checkIO]Element 09.Correct Sentence

우주먼지의하루 2020. 3. 18. 03:28
728x90

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."

 

* 앞 글자가 소문자면 대문자로 바꿔야하고 마지막에 .(온점)을 찍어야한다.

* 앞 글자 이외에 다른 글자는 바꾸면 안됨.

 

- 풀이

 

def correct_sentence(text: str) -> str:
    last = len(text) - 1
    
    if text[0].isupper() == False :
        text=text[0].upper() + text[1:]
      
    if text[last] != "." :
        text = text + "."
        
    return text

 

- 다른 풀이

 

def correct_sentence(text):

    if text[0].islower():
        text = text[0].upper() + text[1:]
    return text if text[-1] == '.' else text + '.'
반응형

'취미생활 > CheckIO' 카테고리의 다른 글

[checkIO]Element 10.Right to Left  (2) 2020.03.21
[checkIO]Element 08.Best Stock  (0) 2020.03.14
[checkIO]Element 07.Even the Last  (0) 2020.03.07
[checkIO]Element 06.Fizz Buzz  (0) 2020.03.03
[checkIO]Element 05.Secret Message  (0) 2020.03.01