diff --git a/algorithms_project/algorithms/arrays/two_pointer.py b/algorithms_project/algorithms/arrays/two_pointer.py index 39c55c0..2b7fd04 100644 --- a/algorithms_project/algorithms/arrays/two_pointer.py +++ b/algorithms_project/algorithms/arrays/two_pointer.py @@ -1,18 +1,18 @@ class Solution: - def reverseWords_manual(s): + def reverseWords_manual(string_word): res = '' - left, r = 0, 0 + left, right = 0, 0 - while r < len(s): - if s[r] != ' ': - r += 1 + while right < len(string_word): + if string_word[right] != ' ': + right += 1 else: - res += s[left:r+1][::-1] - r += 1 - left = r + res += string_word[left:right+1][::-1] + right += 1 + left = right res += ' ' - res += s[left:r + 2][::-1] + res += string_word[left:right + 2][::-1] return res[1:] @@ -32,10 +32,10 @@ def reverserWords(s): class Solution3: def reverseWords_manual1(s): res = '' - for r in range(len(s) + 1): - if r == len(s) or s[r] == ' ': - res += s[:r][::-1] - if r < len(s): + for right in range(len(s) + 1): + if right == len(s) or s[right] == ' ': + res += s[:right][::-1] + if right < len(s): res += ' ' return res diff --git a/algorithms_project/tests/test_arrays/__init__.py b/algorithms_project/tests/test_arrays/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/algorithms_project/tests/test_arrays/test_two_pointer.py b/algorithms_project/tests/test_arrays/test_two_pointer.py new file mode 100644 index 0000000..7c19846 --- /dev/null +++ b/algorithms_project/tests/test_arrays/test_two_pointer.py @@ -0,0 +1,11 @@ +import pytest +from algorithms.arrays.two_pointer import Solution + + +@pytest.mark.parametrize("input_str,expected", [ + ("rac tar", "car rat"), + ("Hello World", "olleH dlroW"), + ("Python", "nohtyP"), +]) +def test_reverse_words(input_str, expected): + assert Solution.reverseWords_manual(input_str) == expected