From 517e2b7dc33e68a8d3fe5b3227ec751c74f61e07 Mon Sep 17 00:00:00 2001 From: Keyboard-1 <142900182+Keyboard-1@users.noreply.github.com> Date: Mon, 9 Oct 2023 00:57:49 +0530 Subject: [PATCH 1/5] Conolidated two scripts reverse_letters.py and reverse_long_words.py into one because of similar functionality --- DIRECTORY.md | 1 - strings/reverse_letters.py | 26 +++++++++++++++----------- strings/reverse_long_words.py | 21 --------------------- 3 files changed, 15 insertions(+), 33 deletions(-) delete mode 100644 strings/reverse_long_words.py diff --git a/DIRECTORY.md b/DIRECTORY.md index a975b9264be0..1ac7ac69c5d8 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1196,7 +1196,6 @@ * [Rabin Karp](strings/rabin_karp.py) * [Remove Duplicate](strings/remove_duplicate.py) * [Reverse Letters](strings/reverse_letters.py) - * [Reverse Long Words](strings/reverse_long_words.py) * [Reverse Words](strings/reverse_words.py) * [Snake Case To Camel Pascal Case](strings/snake_case_to_camel_pascal_case.py) * [Split](strings/split.py) diff --git a/strings/reverse_letters.py b/strings/reverse_letters.py index 10b8a6d72a0f..3f60c87aaffc 100644 --- a/strings/reverse_letters.py +++ b/strings/reverse_letters.py @@ -1,19 +1,23 @@ -def reverse_letters(input_str: str) -> str: +def reverse_letters(sentence: str, length: int = 0) -> str: """ - Reverses letters in a given string without adjusting the position of the words - >>> reverse_letters('The cat in the hat') - 'ehT tac ni eht tah' - >>> reverse_letters('The quick brown fox jumped over the lazy dog.') - 'ehT kciuq nworb xof depmuj revo eht yzal .god' - >>> reverse_letters('Is this true?') - 'sI siht ?eurt' - >>> reverse_letters("I love Python") - 'I evol nohtyP' + 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 432112345 123456' + >>> reverse_letters("To be or not to be") + 'oT eb ro ton ot eb' """ - return " ".join([word[::-1] for word in input_str.split()]) + 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")) diff --git a/strings/reverse_long_words.py b/strings/reverse_long_words.py deleted file mode 100644 index 39ef11513f40..000000000000 --- a/strings/reverse_long_words.py +++ /dev/null @@ -1,21 +0,0 @@ -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")) From 725a1817961f2a2e6892d9f9bc07e7687fd8b652 Mon Sep 17 00:00:00 2001 From: Keyboard-1 <142900182+Keyboard-1@users.noreply.github.com> Date: Mon, 9 Oct 2023 01:06:40 +0530 Subject: [PATCH 2/5] Added a new line to accomodate characters without going over 88 char limit --- strings/reverse_letters.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/strings/reverse_letters.py b/strings/reverse_letters.py index 3f60c87aaffc..4ad0b2903e36 100644 --- a/strings/reverse_letters.py +++ b/strings/reverse_letters.py @@ -1,6 +1,7 @@ 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 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' From 2a86ec7728e58242c98b37398ceebad26c24b669 Mon Sep 17 00:00:00 2001 From: Keyboard-1 <142900182+Keyboard-1@users.noreply.github.com> Date: Mon, 9 Oct 2023 01:11:50 +0530 Subject: [PATCH 3/5] fixed grammar to pass pre-commit --- strings/reverse_letters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings/reverse_letters.py b/strings/reverse_letters.py index 4ad0b2903e36..104af5147946 100644 --- a/strings/reverse_letters.py +++ b/strings/reverse_letters.py @@ -9,8 +9,8 @@ def reverse_letters(sentence: str, length: int = 0) -> str: 'Python is Python' >>> reverse_letters("1 12 123 1234 54321 654321", 0) '1 21 321 432112345 123456' - >>> reverse_letters("To be or not to be") - 'oT eb ro ton ot eb' + >>> reverse_letters("oT be or not to be") + 'To eb ro ton ot eb' """ return " ".join( "".join(word[::-1]) if len(word) > length else word for word in sentence.split() From 45b9f0f36446b93126a580549bca3059e81df9fe Mon Sep 17 00:00:00 2001 From: Keyboard-1 <142900182+Keyboard-1@users.noreply.github.com> Date: Mon, 9 Oct 2023 01:14:59 +0530 Subject: [PATCH 4/5] Changed faulty test case entirely to pass pre commit --- strings/reverse_letters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings/reverse_letters.py b/strings/reverse_letters.py index 104af5147946..c72d971ca37a 100644 --- a/strings/reverse_letters.py +++ b/strings/reverse_letters.py @@ -9,8 +9,8 @@ def reverse_letters(sentence: str, length: int = 0) -> str: 'Python is Python' >>> reverse_letters("1 12 123 1234 54321 654321", 0) '1 21 321 432112345 123456' - >>> reverse_letters("oT be or not to be") - 'To eb ro ton ot eb' + >>> reverse_letters("racecar") + 'racecar' """ return " ".join( "".join(word[::-1]) if len(word) > length else word for word in sentence.split() From 62fca44cfd307a73f3c2b37016aa6a83ba0cdb81 Mon Sep 17 00:00:00 2001 From: Keyboard-1 <142900182+Keyboard-1@users.noreply.github.com> Date: Mon, 9 Oct 2023 01:21:29 +0530 Subject: [PATCH 5/5] fixed a test case which was wrong --- strings/reverse_letters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/reverse_letters.py b/strings/reverse_letters.py index c72d971ca37a..4f73f816b382 100644 --- a/strings/reverse_letters.py +++ b/strings/reverse_letters.py @@ -8,7 +8,7 @@ def reverse_letters(sentence: str, length: int = 0) -> str: >>> reverse_letters("nohtyP is nohtyP", 2) 'Python is Python' >>> reverse_letters("1 12 123 1234 54321 654321", 0) - '1 21 321 432112345 123456' + '1 21 321 4321 12345 123456' >>> reverse_letters("racecar") 'racecar' """