diff --git a/algorithms_project/algorithms/arrays/two_pointer.py b/algorithms_project/algorithms/arrays/two_pointer.py index 2b7fd04..a124a37 100644 --- a/algorithms_project/algorithms/arrays/two_pointer.py +++ b/algorithms_project/algorithms/arrays/two_pointer.py @@ -30,13 +30,15 @@ def reverserWords(s): class Solution3: - def reverseWords_manual1(s): + def reverseWords_manual1(sword: str) -> str: res = '' - for right in range(len(s) + 1): - if right == len(s) or s[right] == ' ': - res += s[:right][::-1] - if right < len(s): + left = 0 + for right in range(len(sword) + 1): + if right == len(sword) or sword[right] == ' ': + res += sword[left:right][::-1] + if right < len(sword): res += ' ' + left = right + 1 return res diff --git a/algorithms_project/tests/test_arrays/test_two_pointer.py b/algorithms_project/tests/test_arrays/test_two_pointer.py index 7c19846..45c5cdb 100644 --- a/algorithms_project/tests/test_arrays/test_two_pointer.py +++ b/algorithms_project/tests/test_arrays/test_two_pointer.py @@ -1,5 +1,5 @@ import pytest -from algorithms.arrays.two_pointer import Solution +from algorithms.arrays.two_pointer import Solution, Solution2, Solution3 @pytest.mark.parametrize("input_str,expected", [ @@ -9,3 +9,16 @@ ]) def test_reverse_words(input_str, expected): assert Solution.reverseWords_manual(input_str) == expected + + +@pytest.mark.parametrize("input_str,expected", [ + ("I evol edocteel", "I love leetcode"), +]) +def test_reverse_words1(input_str, expected): + assert Solution2.reverserWords(input_str) == expected + +@pytest.mark.parametrize("input_str,expected", [ + ("Let's take LeetCode contest", "s'teL ekat edoCteeL tsetnoc"), +]) +def test_reverse_words2(input_str, expected): + assert Solution3.reverseWords_manual1(input_str) == expected \ No newline at end of file