-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_words.py
46 lines (34 loc) · 958 Bytes
/
reverse_words.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
#
# https://leetcode.com/problems/reverse-words-in-a-string/
#
def reverse_words2(inp):
w = []
end_idx = len(inp) - 1
word_start = False
for idx in xrange(len(inp) - 1, -1, -1):
if inp[idx].isspace():
if word_start:
w.append(inp[idx + 1:end_idx + 1])
word_start = False
elif not word_start:
word_start = True
end_idx = idx
if word_start:
w.append(inp[:end_idx + 1])
return " ".join(w)
def reverse_words(s):
w = []
cur_word = []
for l in xrange(len(s) - 1, -1, -1):
if not s[l].isspace():
cur_word.append(s[l])
elif cur_word:
w.append("".join(cur_word)[::-1])
cur_word = []
if cur_word:
w.append("".join(cur_word)[::-1])
return " ".join(w)
inp1 = "a good example"
inp2 = " hello world! "
inp3 = "the sky is blue"
print reverse_words2(inp3), "!"