11
11
import unittest
12
12
13
13
14
- def check_is_unique_input (input_str ) :
14
+ def _validate_input_str (input_str : str ) -> None :
15
15
"""
16
16
Does some input checking for the is_unique function and its variants
17
17
Raises exceptions if input_str is empty or if input_str is not of type string
18
18
:param input_str: a string that we are checking to make
19
19
:return:
20
20
"""
21
- if not isinstance (input_str , str ):
22
- raise TypeError ("expected string as input" )
23
21
if input_str == "" :
24
22
raise ValueError ("empty input string" )
25
23
26
24
27
25
def is_unique (input_str ):
28
26
"""
29
27
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)
32
32
An exception will be raised to handle faulty input.
33
33
Given: Expect:
34
34
tacos True
35
35
swag True
36
36
bobby False
37
37
california False
38
38
orbit True
39
- e true
39
+ e True
40
40
:param input_str: the string we want to check characters of
41
41
:return: returns True if input_str has all unique characters, False otherwise
42
42
"""
43
- check_is_unique_input (input_str )
43
+ _validate_input_str (input_str )
44
44
chars_seen = set ()
45
45
for c in input_str :
46
46
if c in chars_seen :
@@ -52,6 +52,8 @@ def is_unique(input_str):
52
52
def is_unique_no_additional_data_structures (input_str ):
53
53
"""
54
54
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.
55
57
Given: Expect:
56
58
tacos True
57
59
swag True
@@ -62,39 +64,29 @@ def is_unique_no_additional_data_structures(input_str):
62
64
:param input_str: the string we want to check characters of
63
65
:return: returns True if input_str has all unique characters, False otherwise
64
66
"""
65
- check_is_unique_input (input_str )
67
+ _validate_input_str (input_str )
66
68
for i , c in enumerate (input_str ):
67
69
if c in input_str [i + 1 :]:
68
70
return False
69
71
return True
70
72
71
73
72
74
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 )
81
80
with self .assertRaises (TypeError ):
82
- is_unique (8 )
81
+ f (8 )
83
82
with self .assertRaises (ValueError ):
84
- is_unique ("" )
83
+ f ("" )
84
+
85
+ def test_is_unique (self ):
86
+ self ._run_tests (is_unique )
85
87
86
88
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 )
98
90
99
91
100
92
if __name__ == '__main__' :
0 commit comments