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
12 changes: 7 additions & 5 deletions algorithms_project/algorithms/arrays/two_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
15 changes: 14 additions & 1 deletion algorithms_project/tests/test_arrays/test_two_pointer.py
Original file line number Diff line number Diff line change
@@ -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", [
Expand All @@ -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
Loading