1
1
"""
2
2
Python version 3.7.0
3
3
1.3 - URLify
4
- Write a method to replace all spaces in a string with "%20". You may assume that the string has sufficient
5
- space at the end to hold the additional characters, and that you are given the "true" length of the string.
4
+ Write a method to replace all spaces in a string with "%20".
5
+ The following only applies to C/C++: "You may assume that the string has sufficient
6
+ space at the end to hold the additional characters, and that you are given the "true" length of the string"
6
7
(Note: If implementing in Java, please use a character array so that you can perform this operation in place)
8
+ Since we are using python, no need to use true length. I will have two functions, one with
9
+ true_length and the other without.
7
10
"""
8
11
import unittest
9
12
from typing import Callable
@@ -32,8 +35,29 @@ def urlify(s: str, true_length: int) -> str:
32
35
return '' .join (output )
33
36
34
37
38
+ def urlify_no_true_length (s : str ) -> str :
39
+ """
40
+ This is the version of urlify without the true_length argument.
41
+ Given a string and it's "true" length, this function will return a new string
42
+ that replaces all of the spaces of the input string with '%20'.
43
+ Note that this will return a different result than the other urlify
44
+ if the other urlify's true length truncates the string with chars left over.
45
+ Precondition(s):
46
+ - length of s <= true_length
47
+ :param s: the original string to 'urlify'
48
+ :return: string with each space from s replaced with '%20'
49
+ """
50
+ output = []
51
+ for c in s :
52
+ if c == ' ' :
53
+ output .append ("%20" )
54
+ continue
55
+ output .append (c )
56
+ return '' .join (output )
57
+
58
+
35
59
class TestUrlifyFunction (unittest .TestCase ):
36
- def _run_tests (self , f : Callable [[str , int ], str ]) -> None :
60
+ def _run_tests (self , f1 : Callable [[str , int ], str ], f2 : Callable [[ str ], str ]) -> None :
37
61
cases = [
38
62
(("Mr John Smith " , 13 ), "Mr%20John%20Smith" ),
39
63
(("Miguel Hernandez" , 16 ), "Miguel%20Hernandez" ),
@@ -43,11 +67,22 @@ def _run_tests(self, f: Callable[[str, int], str]) -> None:
43
67
(("ihavenospaces" , 13 ), "ihavenospaces" ),
44
68
(("nospacesIgnoreme" , 8 ), "nospaces" )
45
69
]
70
+ cases_no_true_length = [
71
+ ("Mr John Smith" , "Mr%20John%20Smith" ),
72
+ ("Miguel Hernandez" , "Miguel%20Hernandez" ),
73
+ (" Techqueria " , "%20Techqueria%20" ),
74
+ ("a b c d e f g h" , "a%20b%20c%20d%20e%20f%20g%20h" ),
75
+ ("a b c d e f g h ignore this" , "a%20b%20c%20d%20e%20f%20g%20h%20ignore%20this" ),
76
+ ("ihavenospaces" , "ihavenospaces" ),
77
+ ("nospacesIgnoreme" , "nospacesIgnoreme" )
78
+ ]
46
79
for args , expected in cases :
47
- self .assertEqual (f (* args ), expected , msg = args )
80
+ self .assertEqual (f1 (* args ), expected , msg = args )
81
+ for s , expected in cases_no_true_length :
82
+ self .assertEqual (f2 (s ), expected , msg = s )
48
83
49
84
def test_urlify (self ):
50
- self ._run_tests (urlify )
85
+ self ._run_tests (urlify , urlify_no_true_length )
51
86
52
87
53
88
if __name__ == '__main__' :
0 commit comments