-
Notifications
You must be signed in to change notification settings - Fork 3
/
replace_spaces.py
executable file
·66 lines (54 loc) · 1.61 KB
/
replace_spaces.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
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
"""
Write a function to replace all spaces in a text string with '%20'.
"""
def solution1(text):
"""
Multi-pass solution O(n).
>>> solution1("let's replace all whitespaces with %20")
"let's%20replace%20all%20whitespaces%20with%20%20"
"""
# Count space characters in input string
text = list(text)
old_len = len(text)
space_count = sum(map(lambda x: 1 if x == ' ' else 0, text))
# Expand input string to accommodate for %20
for i in range(space_count * 2):
text.append("_") # dummy character
# Traverse the string in reverse and replace characters accordingly
idx = len(text)-1
for i in range(old_len-1, -1, -1):
if text[i] == ' ':
text[idx] = '0'
text[idx-1] = '2'
text[idx-2] = '%'
idx -= 3
else:
text[idx] = text[i]
idx -= 1
return "".join(text)
def solution2(text):
"""
Single forward pass w/ queue solution O(n).
>>> solution1("let's replace all whitespaces with %20")
"let's%20replace%20all%20whitespaces%20with%20%20"
"""
from collections import dequeue
text = list(text)
queue = dequeue()
old_len, idx = len(text), 0
for i in range(old_len):
if text[i] == ' ':
queue.appendleft('%')
queue.appendleft('2')
queue.appendleft('0')
else:
queue.appendleft(text[i])
text[idx] = queue.pop()
idx += 1
while queue:
text.append(queue.pop())
return "".join(text)
if __name__ == "__main__":
import doctest
doctest.testmod()