Skip to content

Commit 63d53aa

Browse files
add solution for urlify.py
1 parent af11152 commit 63d53aa

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

URLify/urlify.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Replace all spaces in a string with '%20'
3+
4+
output - string with spaces replaced with '%20'
5+
input - string
6+
constraints - O(n)
7+
examples - 'John Smith ' => 'John%20Smith'
8+
edge cases
9+
non-string input => throw an error
10+
all spaces in a string => return empty string
11+
more than one space between words => handle them accordingly
12+
"""
13+
import re
14+
15+
def urlify(inputString):
16+
# If input is not a string
17+
if type(inputString) != str:
18+
# Raise an error
19+
raise ValueError('Input is not a string')
20+
# Return RegEx expression that replaces spaces in the string (which has been stripped)
21+
return re.sub(" ", '%20', inputString.strip())

0 commit comments

Comments
 (0)