Skip to content

Commit 3f016f2

Browse files
authored
Merge pull request #147 from BrianLusina/feat/algorithms-two-pointers-reverse-words
refactor(algorithms two pointers): reverse words
2 parents 2933237 + dc40e7a commit 3f016f2

File tree

7 files changed

+61
-68
lines changed

7 files changed

+61
-68
lines changed

DIRECTORY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@
222222
* [Test Trapped Rain Water](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/rain_water_trapped/test_trapped_rain_water.py)
223223
* Reverse Array
224224
* [Test Reverse Array](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/reverse_array/test_reverse_array.py)
225+
* Reverse Words
226+
* [Test Reverse Words](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/reverse_words/test_reverse_words.py)
225227
* Sort Colors
226228
* [Test Sort Colors](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/sort_colors/test_sort_colors.py)
227229
* Three Sum
@@ -865,8 +867,6 @@
865867
* [Test Check Permutation](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/permutation/test_check_permutation.py)
866868
* Reverse Vowels
867869
* [Test Reverse Vowels](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/reverse_vowels/test_reverse_vowels.py)
868-
* Reverse Words
869-
* [Test Reverse Words](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/reverse_words/test_reverse_words.py)
870870
* Similar String Groups
871871
* [Test Similar String Groups](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/similar_string_groups/test_similar_string_groups.py)
872872
* Spreadsheet Encoding
File renamed without changes.

pystrings/reverse_words/__init__.py renamed to algorithms/two_pointers/reverse_words/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,15 @@ def reverse_characters(message_list: List[str], front_index: int, back_index: in
4141
back_index -= 1
4242

4343
return message_list
44+
45+
46+
def reverse_words_two_pointers(sentence: str) -> str:
47+
result = sentence.split()
48+
left = 0
49+
right = len(result) - 1
50+
while left <= right:
51+
result[left], result[right] = result[right], result[left]
52+
left += 1
53+
right -= 1
54+
55+
return " ".join(result)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
from parameterized import parameterized
3+
from algorithms.two_pointers.reverse_words import (
4+
reverse_words,
5+
reverse_words_two_pointers,
6+
)
7+
8+
REVERSE_WORDS_TEST_CASES = [
9+
("vault", "vault"),
10+
("thief cake", "cake thief"),
11+
("one another get", "get another one"),
12+
("rat the ate cat the", "the cat ate the rat"),
13+
("yummy is cake bundt chocolate", "chocolate bundt cake is yummy"),
14+
("", ""),
15+
(" hello world ", "world hello"),
16+
("a good example", "example good a"),
17+
("We love Python ", "Python love We"),
18+
("1234 abc XYZ", "XYZ abc 1234"),
19+
("You are amazing", "amazing are You"),
20+
("Hello World", "World Hello"),
21+
(" Greeting123 ", "Greeting123"),
22+
]
23+
24+
25+
class ReverseWordsTests(unittest.TestCase):
26+
@parameterized.expand(REVERSE_WORDS_TEST_CASES)
27+
def test_reverse_words(self, message: str, expected: str):
28+
actual = reverse_words(message)
29+
self.assertEqual(expected, actual)
30+
31+
@parameterized.expand(REVERSE_WORDS_TEST_CASES)
32+
def test_reverse_words_two_pointers(self, message: str, expected: str):
33+
actual = reverse_words_two_pointers(message)
34+
self.assertEqual(expected, actual)
35+
36+
37+
if __name__ == "__main__":
38+
unittest.main()
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
def count_bits(n: int) -> int:
2-
count = 0
2+
count = 0
33

4-
while n:
5-
# Check the least significant bit by using AND
6-
if n & 1:
7-
count += 1
8-
# Right-shift the number to move to the next bit
9-
n >>= 1
4+
while n:
5+
# Check the least significant bit by using AND
6+
if n & 1:
7+
count += 1
8+
# Right-shift the number to move to the next bit
9+
n >>= 1
1010

11-
return count
11+
return count

bit_manipulation/number_of_1_bits/test_number_of_one_bits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ def test_count_bits(self, _, n: int, expected: int):
1919
self.assertEqual(expected, actual)
2020

2121

22-
if __name__ == '__main__':
22+
if __name__ == "__main__":
2323
unittest.main()

pystrings/reverse_words/test_reverse_words.py

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)