-
Notifications
You must be signed in to change notification settings - Fork 134
/
Sentence_Reverse.py
42 lines (32 loc) · 1.18 KB
/
Sentence_Reverse.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
"""[ 'p', 'e', 'r', 'f', 'e', 'c', 't', ' ', 'm', 'a', 'k', 'e', 's', ' ', 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ]
would turn into:
[ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ', 'm', 'a', 'k', 'e', 's', ' ', 'p', 'e', 'r', 'f', 'e', 'c', 't' ]"""
def reverse_sentence(arr):
# reverse all characters:
n = len(arr)
mirrorReverse(arr, 0, n-1)
# reverse each word:
word_start = None
for i in range(0, n):
if arr[i] == " ":
if word_start is not None:
mirrorReverse(arr, word_start, i-1)
word_start = None
elif i == n-1:
if word_start is not None:
mirrorReverse(arr, word_start, i)
else:
if word_start is None:
word_start = i
def mirrorReverse(arr, start, end):
while start < end:
tmp = arr[start]
arr[start] = arr[end]
arr[end] = tmp
start += 1
end -= 1
if __name__ == '__main__':
arr = ['p', 'e', 'r', 'f', 'e', 'c', 't', ' ', 'm', 'a', 'k', 'e', 's', ' ', 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e']
print('Before reverse:', ''.join(arr))
reverse_sentence(arr)
print('After reverse: ', ''.join(arr))