Skip to content

Commit dc8597d

Browse files
committed
condensed test code, other changes reflect comments on PR review
1 parent 17f8665 commit dc8597d

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

Python/chapter01/1.1 - Is Unique/miguel_1.1_sol.py

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,36 @@
1111
import unittest
1212

1313

14-
def check_is_unique_input(input_str):
14+
def _validate_input_str(input_str: str) -> None:
1515
"""
1616
Does some input checking for the is_unique function and its variants
1717
Raises exceptions if input_str is empty or if input_str is not of type string
1818
:param input_str: a string that we are checking to make
1919
:return:
2020
"""
21-
if not isinstance(input_str, str):
22-
raise TypeError("expected string as input")
2321
if input_str == "":
2422
raise ValueError("empty input string")
2523

2624

2725
def is_unique(input_str):
2826
"""
2927
Determines if input_str has all unique characters.
30-
input string MUST have at least 1 character. n>=1, where n is the number of characters in a string.
31-
Hence, the domain is now defined. Any input not within the domain will not be considered. (n < 1)
28+
For all characters in the input_str with n >= 1, where n is the number of characters in input_str,
29+
there must be no duplicate characters.
30+
In the worst case, the run time of this function will be O(n)
31+
Space complexity will be O(n)
3232
An exception will be raised to handle faulty input.
3333
Given: Expect:
3434
tacos True
3535
swag True
3636
bobby False
3737
california False
3838
orbit True
39-
e true
39+
e True
4040
:param input_str: the string we want to check characters of
4141
:return: returns True if input_str has all unique characters, False otherwise
4242
"""
43-
check_is_unique_input(input_str)
43+
_validate_input_str(input_str)
4444
chars_seen = set()
4545
for c in input_str:
4646
if c in chars_seen:
@@ -52,6 +52,8 @@ def is_unique(input_str):
5252
def is_unique_no_additional_data_structures(input_str):
5353
"""
5454
Variant of is_unique. Uses no additional data structures (besides the variables from the iterator)
55+
However, the drawback is that the runtime is O((n*n-1)/2) = O(n^2), where n is the number of characters
56+
in the input_str.
5557
Given: Expect:
5658
tacos True
5759
swag True
@@ -62,39 +64,29 @@ def is_unique_no_additional_data_structures(input_str):
6264
:param input_str: the string we want to check characters of
6365
:return: returns True if input_str has all unique characters, False otherwise
6466
"""
65-
check_is_unique_input(input_str)
67+
_validate_input_str(input_str)
6668
for i, c in enumerate(input_str):
6769
if c in input_str[i+1:]:
6870
return False
6971
return True
7072

7173

7274
class TestIsUniqueFunction(unittest.TestCase):
73-
def test_is_unique(self):
74-
self.assertFalse(is_unique("techqueria"))
75-
self.assertTrue(is_unique("tacos"))
76-
self.assertTrue(is_unique("swag"))
77-
self.assertFalse(is_unique("bobby"))
78-
self.assertFalse(is_unique("california"))
79-
self.assertTrue(is_unique("orbit"))
80-
self.assertTrue(is_unique("e"))
75+
def _run_tests(self, f: callable([[str], None])) -> None:
76+
for case in ["techqueria", "bobby", "california"]:
77+
self.assertFalse(f(case), msg=case)
78+
for case in ["tacos", "swag", "orbit", "e"]:
79+
self.assertTrue(f(case), msg=case)
8180
with self.assertRaises(TypeError):
82-
is_unique(8)
81+
f(8)
8382
with self.assertRaises(ValueError):
84-
is_unique("")
83+
f("")
84+
85+
def test_is_unique(self):
86+
self._run_tests(is_unique)
8587

8688
def test_is_unique_no_additional_data_structures(self):
87-
self.assertFalse(is_unique_no_additional_data_structures("techqueria"))
88-
self.assertTrue(is_unique_no_additional_data_structures("tacos"))
89-
self.assertTrue(is_unique_no_additional_data_structures("swag"))
90-
self.assertFalse(is_unique_no_additional_data_structures("bobby"))
91-
self.assertFalse(is_unique_no_additional_data_structures("california"))
92-
self.assertTrue(is_unique_no_additional_data_structures("orbit"))
93-
self.assertTrue(is_unique_no_additional_data_structures("e"))
94-
with self.assertRaises(TypeError):
95-
is_unique_no_additional_data_structures(8)
96-
with self.assertRaises(ValueError):
97-
is_unique_no_additional_data_structures("")
89+
self._run_tests(is_unique_no_additional_data_structures)
9890

9991

10092
if __name__ == '__main__':

0 commit comments

Comments
 (0)