Skip to content

Commit 54b4d6d

Browse files
committed
improved string concatentation
1 parent 3b60935 commit 54b4d6d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ def urlify(s: str, true_length: int) -> str:
1919
:param true_length: since s may have additional characters, we focus on true_length instead of actual s length
2020
:return: string with each space from s replaced with '%20'
2121
"""
22-
output = ""
22+
# Below link goes over string concat efficiency in python (I will use method 4
23+
# https://waymoot.org/home/python_string/
24+
output = []
2325
for i, c in enumerate(s):
2426
if i == true_length:
2527
break
2628
if c == ' ':
27-
output += "%20"
29+
output.append("%20")
2830
continue
29-
output += c
30-
return output
31+
output.append(c)
32+
return ''.join(output)
3133

3234

3335
class TestUrlifyFunction(unittest.TestCase):

0 commit comments

Comments
 (0)