Skip to content

Commit 93e2a71

Browse files
committed
update with no true length function, new slightly modded tests, comments explaining why no true length
1 parent 54b4d6d commit 93e2a71

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"""
22
Python version 3.7.0
33
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"
67
(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.
710
"""
811
import unittest
912
from typing import Callable
@@ -32,8 +35,29 @@ def urlify(s: str, true_length: int) -> str:
3235
return ''.join(output)
3336

3437

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+
3559
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:
3761
cases = [
3862
(("Mr John Smith ", 13), "Mr%20John%20Smith"),
3963
(("Miguel Hernandez", 16), "Miguel%20Hernandez"),
@@ -43,11 +67,22 @@ def _run_tests(self, f: Callable[[str, int], str]) -> None:
4367
(("ihavenospaces", 13), "ihavenospaces"),
4468
(("nospacesIgnoreme", 8), "nospaces")
4569
]
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+
]
4679
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)
4883

4984
def test_urlify(self):
50-
self._run_tests(urlify)
85+
self._run_tests(urlify, urlify_no_true_length)
5186

5287

5388
if __name__ == '__main__':

0 commit comments

Comments
 (0)