Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1ef1db7

Browse files
committed
feat: add reverse words
1 parent 4ed936a commit 1ef1db7

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

‎src/array_string/reverse_words_in_a_string/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def reverseWords(self, s: str) -> str:
3+
words = s.split()
4+
left, right = 0, len(words) - 1
5+
6+
while left < right:
7+
words[left], words[right] = words[right], words[left]
8+
left += 1
9+
right -= 1
10+
11+
return " ".join(words)

‎tests/test_reverse_words.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
from src.array_string.reverse_words_in_a_string.solution import Solution
3+
4+
5+
@pytest.mark.parametrize(
6+
"s, expected",
7+
[
8+
("the sky is blue", "blue is sky the"),
9+
(" hello world ", "world hello"),
10+
("a good example", "example good a"),
11+
],
12+
)
13+
def test_reverse_words(s, expected):
14+
solution = Solution()
15+
assert solution.reverseWords(s) == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /