forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_114.py
53 lines (42 loc) · 1.6 KB
/
problem_114.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
47
48
49
50
51
52
53
def reverse_words(string, delimiters):
words = list()
delims = list()
delim_positions = list() # stores positions of the delimiters seen
start = 0
i = 0
while i < len(string):
char = string[i]
if char in delimiters:
word = string[start:i]
if i - start > 1:
words.append(word)
delims.append(char)
delim_positions.append(len(words) + len(delims) - 1)
start = i + 1
i += 1
# get last word if present
if i - start > 1:
words.append(string[start:i])
words.reverse() # reverse just the words
reversed_order = list()
word_index = 0
delim_index = 0
# merging the reversed words and the delimiters
for i in range(len(words) + len(delims)):
if delim_index < len(delim_positions) and delim_positions[delim_index] == i:
# insert next delimiter if the position is saved for a delimiter
reversed_order.append(delims[delim_index])
delim_index += 1
else:
reversed_order.append(words[word_index])
word_index += 1
reversed_string = "".join(reversed_order)
return reversed_string
assert reverse_words("hello/world:here/",
set([':', '/'])) == "here/world:hello/"
assert reverse_words(":hello//world:here/",
set([':', '/'])) == ":here//world:hello/"
assert reverse_words("hello//world:here",
set([':', '/'])) == "here//world:hello"
assert reverse_words("hello/world:here",
set([':', '/'])) == "here/world:hello"