mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
66e4ea6a62
* Conolidated two scripts reverse_letters.py and reverse_long_words.py into one because of similar functionality * Added a new line to accomodate characters without going over 88 char limit * fixed grammar to pass pre-commit * Changed faulty test case entirely to pass pre commit * fixed a test case which was wrong --------- Co-authored-by: Keyboard-1 <142900182+Keyboard-1@users.noreply.github.com>
25 lines
729 B
Python
25 lines
729 B
Python
def reverse_letters(sentence: str, length: int = 0) -> str:
|
|
"""
|
|
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'
|
|
"""
|
|
return " ".join(
|
|
"".join(word[::-1]) if len(word) > length else word for word in sentence.split()
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|
|
print(reverse_letters("Hey wollef sroirraw"))
|