9
9
true_length and the other without.
10
10
"""
11
11
import unittest
12
+ from typing import List
12
13
13
14
14
- def urlify (s : str , true_length : int ) -> str :
15
+ def urlify (s : str ) -> str :
15
16
"""
16
- Given a string and it's "true" length , this function will return a new string
17
+ Given a string, this function will return a new string
17
18
that replaces all of the spaces of the input string with '%20'.
18
- Precondition(s):
19
- - length of s <= true_length
20
19
:param s: the original string to 'urlify'
21
- :param true_length: since s may have additional characters, we focus on true_length instead of actual s length
22
20
:return: string with each space from s replaced with '%20'
23
21
"""
24
- # Below link goes over string concat efficiency in python (I will use method 4
25
- # https://waymoot.org/home/python_string/
26
- # each space will make us replace it (with '%', then add 2 characters ('2', '0')
27
- # must count spaces first to help us find out the size of the
28
- result = ['\x00 ' ] * true_length * 3
29
- space_count = 0
30
- chars_per_space = 2
31
- idx = 0
32
- for x in range (true_length ):
33
- if s [idx - space_count * chars_per_space ] == ' ' :
34
- result [idx ] = '%'
35
- result [idx + 1 ] = '2'
36
- result [idx + 2 ] = '0'
37
- space_count += 1
38
- idx += 3
39
- continue
40
- result [idx ] = s [idx - space_count * chars_per_space ]
41
- idx += 1
42
- return '' .join (result ).rstrip ('\x00 ' )
22
+ buf = ['\x00 ' ] * (len (s ) * 3 )
23
+ for i , c in enumerate (s ):
24
+ buf [i ] = c
25
+
26
+ def _challenge (buf : List [str ], original_length : int ) -> None :
27
+ """
28
+ Your code here. Challenge rules:
29
+ * You can only reference |buf| and |original_length|, not |s|.
30
+ * You can only allocate O(1) additional memory
31
+ * You need to modify |buf| in-place so that the right answer is returned.
32
+ * You cannot modify any part of the wrapper function.
33
+ :param buf: buffer containing characters
34
+ :param original_length: original length of string
35
+ :return: None
36
+ """
37
+ 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'
45
+ space_count += 1
46
+ idx += 3
47
+ continue
48
+ buf [idx ] = s [idx - space_count * chars_per_space ]
49
+ idx += 1
50
+ _challenge (buf , len (s ))
51
+ return '' .join (buf ).rstrip ('\x00 ' )
43
52
44
53
45
54
def urlify_no_true_length (s : str ) -> str :
@@ -66,17 +75,16 @@ def urlify_no_true_length(s: str) -> str:
66
75
class TestUrlifyFunction (unittest .TestCase ):
67
76
def test_urlify (self ):
68
77
cases = [
69
- (("Mr John Smith " , 13 ), "Mr%20John%20Smith" ),
70
- (("Miguel Hernandez" , 16 ), "Miguel%20Hernandez" ),
71
- ((" Techqueria " , 11 ), "%20Techqueria" ),
72
- (("a b c d e f g h" , 15 ), "a%20b%20c%20d%20e%20f%20g%20h" ),
73
- (("a b c d e f g h ignore this" , 15 ), "a%20b%20c%20d%20e%20f%20g%20h" ),
74
- (("ihavenospaces" , 13 ), "ihavenospaces" ),
75
- (("nospacesIgnoreme" , 8 ), "nospaces" ),
76
- ((" " , 1 ), "%20" )
78
+ ("Mr John Smith" , "Mr%20John%20Smith" ),
79
+ ("Miguel Hernandez" , "Miguel%20Hernandez" ),
80
+ (" Techqueria" , "%20Techqueria" ),
81
+ ("a b c d e f g h" , "a%20b%20c%20d%20e%20f%20g%20h" ),
82
+ ("ihavenospaces" , "ihavenospaces" ),
83
+ ("nospaces" , "nospaces" ),
84
+ (" " , "%20" )
77
85
]
78
- for args , expected in cases :
79
- self .assertEqual (urlify (* args ), expected , msg = args )
86
+ for s , expected in cases :
87
+ self .assertEqual (urlify (s ), expected , msg = s )
80
88
81
89
def test_urlify_no_true_length (self ):
82
90
cases = [
0 commit comments