프로그래밍 문제/[Python] CheckIO

CheckIO - correct-sentence

UltraLowTemp-Physics 2020. 12. 6. 15:55

Problems: py.checkio.org/en/mission/correct-sentence/

My Solutions

def correct_sentence(text: str) -> str:
    """
        returns a corrected sentence which starts with a capital letter
        and ends with a dot.
    """
    # your code here
    if not (text[-1] == '.'):
        text = text + '.'     
    text = text[0].upper() + text[1:]
    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!")

 

An example of good solutions that I saw

correct_sentence = lambda text: (text[0].upper() + text[1:] + ('.' if not text.endswith('.') else '')).strip()