Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
38 changes: 38 additions & 0 deletions algorithms/two_pointers/reverse_words/test_reverse_words.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 8 additions & 8 deletions bit_manipulation/number_of_1_bits/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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()
57 changes: 0 additions & 57 deletions pystrings/reverse_words/test_reverse_words.py

This file was deleted.

Loading