Skip to content

Commit 449c20f

Browse files
committed
updated challenge soln (my initial one was wrong)
1 parent 4e3d9b9 commit 449c20f

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Python/chapter01/1.3 - URLify/miguel_1.3_sol.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,21 @@ def _challenge(buf: List[str], original_length: int) -> None:
3535
:return: None
3636
"""
3737
space_count = 0
38-
chars_per_space = 2
39-
idx = 0
40-
for x in range(original_length):
41-
if s[idx - space_count * chars_per_space] == ' ':
42-
buf[idx] = '%'
43-
buf[idx + 1] = '2'
44-
buf[idx + 2] = '0'
38+
for j in range(original_length):
39+
if buf[j] == ' ':
4540
space_count += 1
46-
idx += 3
41+
42+
chars_per_space = 2
43+
idx = original_length + space_count * chars_per_space
44+
for i in range(original_length-1, -1, -1):
45+
if buf[i] == ' ':
46+
buf[idx-1] = '0'
47+
buf[idx-2] = '2'
48+
buf[idx-3] = '%'
49+
idx -= 3
4750
continue
48-
buf[idx] = s[idx - space_count * chars_per_space]
49-
idx += 1
51+
buf[idx-1] = buf[i]
52+
idx -= 1
5053
_challenge(buf, len(s))
5154
return ''.join(buf).rstrip('\x00')
5255

0 commit comments

Comments
 (0)