From c31bcaa752e20bbea808540666392cb174c70f22 Mon Sep 17 00:00:00 2001 From: Mohammad Firmansyah <76118762+dimasdh842@users.noreply.github.com> Date: Mon, 25 Oct 2021 15:00:44 +0000 Subject: [PATCH 1/4] add an algorithm to spin some words --- python_codewars_disemvowel/index.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 python_codewars_disemvowel/index.py diff --git a/python_codewars_disemvowel/index.py b/python_codewars_disemvowel/index.py new file mode 100644 index 000000000000..c1c1c0e8df43 --- /dev/null +++ b/python_codewars_disemvowel/index.py @@ -0,0 +1,29 @@ +""" +This is the Test of SpinWords method. + +TheFunction will spin your words + +>>> spin_words("Hey wollef sroirraw") +'Hey fellow warriors' +""" + +def spin_words(sentence): + sentences = sentence.split(" ") + + for i in range(len(sentences)): + if len(sentences[i]) >= 5: + word = sentences[i] + newWord = "".join([(word[len(word) - e]) for e in range(1,len(sentences[i]) + 1)]) + sentences.remove(sentences[i]) + sentences.insert(i, newWord) + result = " ".join(sentences) + return result + + +print(spin_words("Hey wollef sroirraw")) + + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 6a836c3f1112ff805ab0939c099978175997f840 Mon Sep 17 00:00:00 2001 From: Mohammad Firmansyah <76118762+dimasdh842@users.noreply.github.com> Date: Mon, 25 Oct 2021 15:04:58 +0000 Subject: [PATCH 2/4] Update index.py --- python_codewars_disemvowel/index.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python_codewars_disemvowel/index.py b/python_codewars_disemvowel/index.py index c1c1c0e8df43..9d5f0a26498e 100644 --- a/python_codewars_disemvowel/index.py +++ b/python_codewars_disemvowel/index.py @@ -13,9 +13,9 @@ def spin_words(sentence): for i in range(len(sentences)): if len(sentences[i]) >= 5: word = sentences[i] - newWord = "".join([(word[len(word) - e]) for e in range(1,len(sentences[i]) + 1)]) + new_word = "".join([(word[len(word) - e]) for e in range(1,len(sentences[i]) + 1)]) sentences.remove(sentences[i]) - sentences.insert(i, newWord) + sentences.insert(i, new_word) result = " ".join(sentences) return result From b4ec3985ba7f134d570ba23b246024689133755f Mon Sep 17 00:00:00 2001 From: Mohammad Firmansyah <76118762+dimasdh842@users.noreply.github.com> Date: Mon, 25 Oct 2021 15:08:09 +0000 Subject: [PATCH 3/4] Adding type hint of spin_words function --- python_codewars_disemvowel/index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_codewars_disemvowel/index.py b/python_codewars_disemvowel/index.py index 9d5f0a26498e..47b732712201 100644 --- a/python_codewars_disemvowel/index.py +++ b/python_codewars_disemvowel/index.py @@ -7,7 +7,7 @@ 'Hey fellow warriors' """ -def spin_words(sentence): +def spin_words(sentence: str ) -> str: sentences = sentence.split(" ") for i in range(len(sentences)): From 7c673e06bd46b4782abbc740097c212e6e9a1f81 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 25 Oct 2021 19:13:40 +0200 Subject: [PATCH 4/4] Update and rename python_codewars_disemvowel/index.py to strings/reverse_long_words.py --- python_codewars_disemvowel/index.py | 29 ----------------------------- strings/reverse_long_words.py | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+), 29 deletions(-) delete mode 100644 python_codewars_disemvowel/index.py create mode 100644 strings/reverse_long_words.py diff --git a/python_codewars_disemvowel/index.py b/python_codewars_disemvowel/index.py deleted file mode 100644 index 47b732712201..000000000000 --- a/python_codewars_disemvowel/index.py +++ /dev/null @@ -1,29 +0,0 @@ -""" -This is the Test of SpinWords method. - -TheFunction will spin your words - ->>> spin_words("Hey wollef sroirraw") -'Hey fellow warriors' -""" - -def spin_words(sentence: str ) -> str: - sentences = sentence.split(" ") - - for i in range(len(sentences)): - if len(sentences[i]) >= 5: - word = sentences[i] - new_word = "".join([(word[len(word) - e]) for e in range(1,len(sentences[i]) + 1)]) - sentences.remove(sentences[i]) - sentences.insert(i, new_word) - result = " ".join(sentences) - return result - - -print(spin_words("Hey wollef sroirraw")) - - - -if __name__ == "__main__": - import doctest - doctest.testmod() diff --git a/strings/reverse_long_words.py b/strings/reverse_long_words.py new file mode 100644 index 000000000000..39ef11513f40 --- /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"))