From ac19fc2e27e0f2373962fb1a7f97be152a0f54ef Mon Sep 17 00:00:00 2001 From: NipunRautela Date: Sun, 11 Oct 2020 20:10:02 +0530 Subject: [PATCH 1/4] Added reverse_individual_words.py in strings which reverse each word in the string and return the resulting string. --- strings/reverse_individual_words.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 strings/reverse_individual_words.py diff --git a/strings/reverse_individual_words.py b/strings/reverse_individual_words.py new file mode 100644 index 000000000000..3603c67a8267 --- /dev/null +++ b/strings/reverse_individual_words.py @@ -0,0 +1,19 @@ +def reverse_individual_words(input_str: str) -> str: + """ + Reverses words in a given string + >>> reverse_individual_words("This is a text") + 'sihT si a txet' + >>> reverse_individual_words("Another 1234 text") + 'rehtonA 4321 txet' + """ + individual_reverse_string = [] + for word in input_str.split(" "): + individual_reverse_string.append(word[::-1]) + + return " ".join(individual_reverse_string) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() \ No newline at end of file From 7b22b438bc1af49158a794cd9cbf8268b708eb43 Mon Sep 17 00:00:00 2001 From: NipunRautela Date: Sun, 11 Oct 2020 20:35:58 +0530 Subject: [PATCH 2/4] Added support for end of sentence puntuation in reverse_individual_words.py. --- strings/reverse_individual_words.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/strings/reverse_individual_words.py b/strings/reverse_individual_words.py index 3603c67a8267..7f817f9300f0 100644 --- a/strings/reverse_individual_words.py +++ b/strings/reverse_individual_words.py @@ -5,10 +5,15 @@ def reverse_individual_words(input_str: str) -> str: 'sihT si a txet' >>> reverse_individual_words("Another 1234 text") 'rehtonA 4321 txet' + >>> reverse_individual_words("A sentence with full stop.") + 'A ecnetnes htiw lluf pots.' """ individual_reverse_string = [] for word in input_str.split(" "): - individual_reverse_string.append(word[::-1]) + if word[-1] in ['.', '!', '?']: + individual_reverse_string.append(word[-2::-1]+word[-1]) + else: + individual_reverse_string.append(word[::-1]) return " ".join(individual_reverse_string) From 2d5e037c48be40d1d0590915edc8c44262581121 Mon Sep 17 00:00:00 2001 From: Nipun <63812155+NipunRautela@users.noreply.github.com> Date: Sun, 11 Oct 2020 20:51:11 +0530 Subject: [PATCH 3/4] Fixed some formatting in file --- strings/reverse_individual_words.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings/reverse_individual_words.py b/strings/reverse_individual_words.py index 7f817f9300f0..4dbd494ac6d9 100644 --- a/strings/reverse_individual_words.py +++ b/strings/reverse_individual_words.py @@ -14,11 +14,11 @@ def reverse_individual_words(input_str: str) -> str: individual_reverse_string.append(word[-2::-1]+word[-1]) else: individual_reverse_string.append(word[::-1]) - + return " ".join(individual_reverse_string) if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From 58dd540404c4c037e01cef4da3e4d0babf2898f1 Mon Sep 17 00:00:00 2001 From: NipunRautela Date: Sun, 11 Oct 2020 21:10:45 +0530 Subject: [PATCH 4/4] Implemented Black formatting --- strings/reverse_individual_words.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings/reverse_individual_words.py b/strings/reverse_individual_words.py index 4dbd494ac6d9..29019886bf21 100644 --- a/strings/reverse_individual_words.py +++ b/strings/reverse_individual_words.py @@ -10,8 +10,8 @@ def reverse_individual_words(input_str: str) -> str: """ individual_reverse_string = [] for word in input_str.split(" "): - if word[-1] in ['.', '!', '?']: - individual_reverse_string.append(word[-2::-1]+word[-1]) + if word[-1] in [".", "!", "?"]: + individual_reverse_string.append(word[-2::-1] + word[-1]) else: individual_reverse_string.append(word[::-1])