-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlify.py
More file actions
32 lines (29 loc) · 700 Bytes
/
urlify.py
File metadata and controls
32 lines (29 loc) · 700 Bytes
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
def urlify(string, length):
i = 0
spaces = 0
while i < length:
if string[i] == " ":
spaces = spaces + 1
i = i + 1
i = length - 1
prev_i = length - 1
while i >= 0:
if string[i] == " ":
while i < prev_i:
string[prev_i + 2 * spaces] = string[prev_i]
prev_i = prev_i - 1
string[prev_i + 2 * spaces] = "0"
string[prev_i + 2 * spaces - 1] = "2"
string[prev_i + 2 * spaces - 2] = "%"
spaces = spaces - 1
prev_i = i - 1
i = i - 1
return "".join(a for a in string)
def main():
string = "Mr John Smith"
length = 13
input_string = [a for a in string] + [" " for _ in xrange(4)]
url = urlify(input_string, length)
print(url)
if __name__ == "__main__":
main()