-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc151-reverse-words-in-a-string.py
46 lines (35 loc) · 1.13 KB
/
lc151-reverse-words-in-a-string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Link: https://leetcode.com/problems/reverse-words-in-a-string/
import unittest
class Solution:
def reverseWords(self, s: str) -> str:
words = []
temp_word = ""
for c in s:
if c != " ":
temp_word += c
elif temp_word != "":
words.append(temp_word)
temp_word = ""
if temp_word != "":
words.append(temp_word)
i = 0
j = len(words) - 1
while i < j:
temp = words[i]
words[i] = words[j]
words[j] = temp
i += 1
j -= 1
return " ".join(words)
class TestSolution(unittest.TestCase):
def setUp(self) -> None:
self.solution = Solution()
def test_example_1(self):
s = "the sky is blue"
self.assertEqual("blue is sky the", self.solution.reverseWords(s))
def test_example_2(self):
s = " hello world "
self.assertEqual("world hello", self.solution.reverseWords(s))
def test_example_3(self):
s = "a good example"
self.assertEqual("example good a", self.solution.reverseWords(s))