Skip to content

Commit fce76af

Browse files
committed
refactor(algorithms, two-pointers): reverse words
1 parent 2933237 commit fce76af

File tree

6 files changed

+59
-66
lines changed

6 files changed

+59
-66
lines changed
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)