Reverse Long Words

def reverse_long_words(sentence: str) -> str:
    """
    Reverse all words that are longer than 4 characters in a sentence.

    >>> reverse_long_words("Hey wollef sroirraw")
    'Hey fellow warriors'
    >>> reverse_long_words("nohtyP is nohtyP")
    'Python is Python'
    >>> reverse_long_words("1 12 123 1234 54321 654321")
    '1 12 123 1234 12345 123456'
    """
    return " ".join(
        "".join(word[::-1]) if len(word) > 4 else word for word in sentence.split()
    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    print(reverse_long_words("Hey wollef sroirraw"))
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.