Skip to content

Commit 6b53296

Browse files
URLify string algorithm
1 parent c2d1f39 commit 6b53296

File tree

1 file changed

+28
-0
lines changed
  • Python/chapter01/1.1 - Is Unique

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/python3
2+
'''
3+
Module that Write a method to replace all spaces in a string with '%20'. You may assume that the string
4+
has sufficient space at the end to hold the additional characters, and that you are given the "true"
5+
length of the string. (Note: If implementing in Java, please use a character array so that you can
6+
perform this operation in place.)
7+
'''
8+
9+
import unittest
10+
11+
def URLify(string, char):
12+
'''
13+
Function that replace all spaces in a string with '%20'.
14+
Args:
15+
string (str): a string to modify
16+
'''
17+
return string[:char + 1].replace(" ", "%20")
18+
19+
class Test(unittest.TestCase):
20+
def test1(self):
21+
input_string = "Mr Johns Smith "
22+
char = 13
23+
output_string = "Mr%20John%20Smith"
24+
25+
self.assertTrue(URLify(input_string, 13), output_string)
26+
27+
if __name__ == '__main__':
28+
unittest.main()

0 commit comments

Comments
 (0)