2023-10-08 20:17:22 +00:00
|
|
|
def reverse_letters(sentence: str, length: int = 0) -> str:
|
2020-10-27 04:05:37 +00:00
|
|
|
"""
|
2023-10-08 20:17:22 +00:00
|
|
|
Reverse all words that are longer than the given length of characters in a sentence.
|
|
|
|
If unspecified, length is taken as 0
|
|
|
|
|
|
|
|
>>> reverse_letters("Hey wollef sroirraw", 3)
|
|
|
|
'Hey fellow warriors'
|
|
|
|
>>> reverse_letters("nohtyP is nohtyP", 2)
|
|
|
|
'Python is Python'
|
|
|
|
>>> reverse_letters("1 12 123 1234 54321 654321", 0)
|
|
|
|
'1 21 321 4321 12345 123456'
|
|
|
|
>>> reverse_letters("racecar")
|
|
|
|
'racecar'
|
2020-10-27 04:05:37 +00:00
|
|
|
"""
|
2023-10-08 20:17:22 +00:00
|
|
|
return " ".join(
|
|
|
|
"".join(word[::-1]) if len(word) > length else word for word in sentence.split()
|
|
|
|
)
|
2020-10-27 04:05:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|
2023-10-08 20:17:22 +00:00
|
|
|
print(reverse_letters("Hey wollef sroirraw"))
|