diff --git a/DIRECTORY.md b/DIRECTORY.md index 30b4e665..5bedba9e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -222,6 +222,8 @@ * [Test Trapped Rain Water](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/rain_water_trapped/test_trapped_rain_water.py) * Reverse Array * [Test Reverse Array](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/reverse_array/test_reverse_array.py) + * Reverse Words + * [Test Reverse Words](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/reverse_words/test_reverse_words.py) * Sort Colors * [Test Sort Colors](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/sort_colors/test_sort_colors.py) * Three Sum @@ -865,8 +867,6 @@ * [Test Check Permutation](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/permutation/test_check_permutation.py) * Reverse Vowels * [Test Reverse Vowels](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/reverse_vowels/test_reverse_vowels.py) - * Reverse Words - * [Test Reverse Words](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/reverse_words/test_reverse_words.py) * Similar String Groups * [Test Similar String Groups](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/similar_string_groups/test_similar_string_groups.py) * Spreadsheet Encoding diff --git a/pystrings/reverse_words/README.md b/algorithms/two_pointers/reverse_words/README.md similarity index 100% rename from pystrings/reverse_words/README.md rename to algorithms/two_pointers/reverse_words/README.md diff --git a/pystrings/reverse_words/__init__.py b/algorithms/two_pointers/reverse_words/__init__.py similarity index 83% rename from pystrings/reverse_words/__init__.py rename to algorithms/two_pointers/reverse_words/__init__.py index 12f705f6..f1e39dfe 100755 --- a/pystrings/reverse_words/__init__.py +++ b/algorithms/two_pointers/reverse_words/__init__.py @@ -41,3 +41,15 @@ def reverse_characters(message_list: List[str], front_index: int, back_index: in back_index -= 1 return message_list + + +def reverse_words_two_pointers(sentence: str) -> str: + result = sentence.split() + left = 0 + right = len(result) - 1 + while left <= right: + result[left], result[right] = result[right], result[left] + left += 1 + right -= 1 + + return " ".join(result) diff --git a/algorithms/two_pointers/reverse_words/test_reverse_words.py b/algorithms/two_pointers/reverse_words/test_reverse_words.py new file mode 100644 index 00000000..3a6460c1 --- /dev/null +++ b/algorithms/two_pointers/reverse_words/test_reverse_words.py @@ -0,0 +1,38 @@ +import unittest +from parameterized import parameterized +from algorithms.two_pointers.reverse_words import ( + reverse_words, + reverse_words_two_pointers, +) + +REVERSE_WORDS_TEST_CASES = [ + ("vault", "vault"), + ("thief cake", "cake thief"), + ("one another get", "get another one"), + ("rat the ate cat the", "the cat ate the rat"), + ("yummy is cake bundt chocolate", "chocolate bundt cake is yummy"), + ("", ""), + (" hello world ", "world hello"), + ("a good example", "example good a"), + ("We love Python ", "Python love We"), + ("1234 abc XYZ", "XYZ abc 1234"), + ("You are amazing", "amazing are You"), + ("Hello World", "World Hello"), + (" Greeting123 ", "Greeting123"), +] + + +class ReverseWordsTests(unittest.TestCase): + @parameterized.expand(REVERSE_WORDS_TEST_CASES) + def test_reverse_words(self, message: str, expected: str): + actual = reverse_words(message) + self.assertEqual(expected, actual) + + @parameterized.expand(REVERSE_WORDS_TEST_CASES) + def test_reverse_words_two_pointers(self, message: str, expected: str): + actual = reverse_words_two_pointers(message) + self.assertEqual(expected, actual) + + +if __name__ == "__main__": + unittest.main() diff --git a/bit_manipulation/number_of_1_bits/__init__.py b/bit_manipulation/number_of_1_bits/__init__.py index 9135d0b1..e2a5ad05 100644 --- a/bit_manipulation/number_of_1_bits/__init__.py +++ b/bit_manipulation/number_of_1_bits/__init__.py @@ -1,11 +1,11 @@ def count_bits(n: int) -> int: - count = 0 + count = 0 - while n: - # Check the least significant bit by using AND - if n & 1: - count += 1 - # Right-shift the number to move to the next bit - n >>= 1 + while n: + # Check the least significant bit by using AND + if n & 1: + count += 1 + # Right-shift the number to move to the next bit + n >>= 1 - return count + return count diff --git a/bit_manipulation/number_of_1_bits/test_number_of_one_bits.py b/bit_manipulation/number_of_1_bits/test_number_of_one_bits.py index 2c6676bb..1964654f 100644 --- a/bit_manipulation/number_of_1_bits/test_number_of_one_bits.py +++ b/bit_manipulation/number_of_1_bits/test_number_of_one_bits.py @@ -19,5 +19,5 @@ def test_count_bits(self, _, n: int, expected: int): self.assertEqual(expected, actual) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/pystrings/reverse_words/test_reverse_words.py b/pystrings/reverse_words/test_reverse_words.py deleted file mode 100644 index 34a033b7..00000000 --- a/pystrings/reverse_words/test_reverse_words.py +++ /dev/null @@ -1,57 +0,0 @@ -import unittest - -from . import reverse_words - - -class ReverseWordsTests(unittest.TestCase): - def test_one_word(self): - message = "vault" - actual = reverse_words(message) - expected = "vault" - self.assertEqual(expected, actual) - - def test_two_words(self): - message = "thief cake" - actual = reverse_words(message) - expected = "cake thief" - self.assertEqual(expected, actual) - - def test_three_words(self): - message = "one another get" - actual = reverse_words(message) - expected = "get another one" - self.assertEqual(expected, actual) - - def test_multiple_words_same_length(self): - message = "rat the ate cat the" - actual = reverse_words(message) - expected = "the cat ate the rat" - self.assertEqual(expected, actual) - - def test_multiple_words_different_lengths(self): - message = "yummy is cake bundt chocolate" - actual = reverse_words(message) - expected = "chocolate bundt cake is yummy" - self.assertEqual(expected, actual) - - def test_empty_string(self): - message = "" - actual = reverse_words(message) - expected = "" - self.assertEqual(expected, actual) - - def test_string_with_spaces_at_both_ends(self): - message = " hello world " - actual = reverse_words(message) - expected = "world hello" - self.assertEqual(expected, actual) - - def test_string_with_multiple_spaces_in_between_words(self): - message = "a good example" - actual = reverse_words(message) - expected = "example good a" - self.assertEqual(expected, actual) - - -if __name__ == "__main__": - unittest.main()