7
7
I assume that the user will input a non-empty string
8
8
If the string is empty, then raise a ValueError exception
9
9
If the input is NOT a string, then raise a TypeError exception
10
-
11
- Given: Expect:
12
- tacos true
13
- swag true
14
- bobby false
15
- california false
16
- orbit true
17
- e true
18
10
"""
19
11
import unittest
20
12
21
13
22
14
def check_is_unique_input (input_str ):
15
+ """
16
+ Does some input checking for the is_unique function and its variants
17
+ Raises exceptions if input_str is empty or if input_str is not of type string
18
+ :param input_str: a string that we are checking to make
19
+ :return:
20
+ """
23
21
if not isinstance (input_str , str ):
24
22
raise TypeError ("expected string as input" )
25
23
if input_str == "" :
26
24
raise ValueError ("empty input string" )
27
25
28
26
29
27
def is_unique (input_str ):
28
+ """
29
+ 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)
32
+ An exception will be raised to handle faulty input.
33
+ Given: Expect:
34
+ tacos True
35
+ swag True
36
+ bobby False
37
+ california False
38
+ orbit True
39
+ e true
40
+ :param input_str: the string we want to check characters of
41
+ :return: returns True if input_str has all unique characters, False otherwise
42
+ """
30
43
check_is_unique_input (input_str )
31
44
chars_seen = set ()
32
45
for c in input_str :
@@ -37,6 +50,18 @@ def is_unique(input_str):
37
50
38
51
39
52
def is_unique_no_additional_data_structures (input_str ):
53
+ """
54
+ Variant of is_unique. Uses no additional data structures (besides the variables from the iterator)
55
+ Given: Expect:
56
+ tacos True
57
+ swag True
58
+ bobby False
59
+ california False
60
+ orbit True
61
+ e True
62
+ :param input_str: the string we want to check characters of
63
+ :return: returns True if input_str has all unique characters, False otherwise
64
+ """
40
65
check_is_unique_input (input_str )
41
66
for i , c in enumerate (input_str ):
42
67
if c in input_str [i + 1 :]:
0 commit comments