Skip to content

Commit 74e442e

Browse files
dimasdh842cclauss
andauthored
add an algorithm to spin some words (#5597)
* add an algorithm to spin some words * Update index.py * Adding type hint of spin_words function * Update and rename python_codewars_disemvowel/index.py to strings/reverse_long_words.py Co-authored-by: Christian Clauss <[email protected]>
1 parent b55da04 commit 74e442e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

strings/reverse_long_words.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def reverse_long_words(sentence: str) -> str:
2+
"""
3+
Reverse all words that are longer than 4 characters in a sentence.
4+
5+
>>> reverse_long_words("Hey wollef sroirraw")
6+
'Hey fellow warriors'
7+
>>> reverse_long_words("nohtyP is nohtyP")
8+
'Python is Python'
9+
>>> reverse_long_words("1 12 123 1234 54321 654321")
10+
'1 12 123 1234 12345 123456'
11+
"""
12+
return " ".join(
13+
"".join(word[::-1]) if len(word) > 4 else word for word in sentence.split()
14+
)
15+
16+
17+
if __name__ == "__main__":
18+
import doctest
19+
20+
doctest.testmod()
21+
print(reverse_long_words("Hey wollef sroirraw"))

0 commit comments

Comments
 (0)