From 74e442e979b1ffdbafa97765193ec04058893fca Mon Sep 17 00:00:00 2001 From: Mohammad Firmansyah <76118762+dimasdh842@users.noreply.github.com> Date: Mon, 25 Oct 2021 17:18:41 +0000 Subject: [PATCH] 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 --- strings/reverse_long_words.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 strings/reverse_long_words.py diff --git a/strings/reverse_long_words.py b/strings/reverse_long_words.py new file mode 100644 index 000000000..39ef11513 --- /dev/null +++ b/strings/reverse_long_words.py @@ -0,0 +1,21 @@ +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"))