취미생활/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 + '.'
반응형