Skip to content

Commit 22e95b5

Browse files
committed
empty str returns true, using types in function signature
1 parent 5e935d8 commit 22e95b5

File tree

1 file changed

+5
-19
lines changed

1 file changed

+5
-19
lines changed

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,24 @@
1212
from typing import Callable
1313

1414

15-
def _validate_input_str(input_str: str) -> None:
16-
"""
17-
Does some input checking for the is_unique function and its variants
18-
Raises exceptions if input_str is empty or if input_str is not of type string
19-
:param input_str: a string that we are checking to make
20-
:return:
21-
"""
22-
if input_str == "":
23-
raise ValueError("empty input string")
24-
25-
26-
def is_unique(input_str):
15+
def is_unique(input_str: str) -> bool:
2716
"""
2817
Determines if input_str has all unique characters.
2918
For all characters in the input_str with n >= 1, where n is the number of characters in input_str,
3019
there must be no duplicate characters.
3120
In the worst case, the run time of this function will be O(n)
3221
Space complexity will be O(n)
33-
An exception will be raised to handle faulty input.
3422
Given: Expect:
3523
tacos True
3624
swag True
3725
bobby False
3826
california False
3927
orbit True
4028
e True
29+
"" True
4130
:param input_str: the string we want to check characters of
4231
:return: returns True if input_str has all unique characters, False otherwise
4332
"""
44-
_validate_input_str(input_str)
4533
chars_seen = set()
4634
for c in input_str:
4735
if c in chars_seen:
@@ -50,7 +38,7 @@ def is_unique(input_str):
5038
return True
5139

5240

53-
def is_unique_no_additional_data_structures(input_str):
41+
def is_unique_no_additional_data_structures(input_str: str) -> bool:
5442
"""
5543
Variant of is_unique. Uses no additional data structures (besides the variables from the iterator)
5644
However, the drawback is that the runtime is O((n*n-1)/2) = O(n^2), where n is the number of characters
@@ -62,10 +50,10 @@ def is_unique_no_additional_data_structures(input_str):
6250
california False
6351
orbit True
6452
e True
53+
"" True
6554
:param input_str: the string we want to check characters of
6655
:return: returns True if input_str has all unique characters, False otherwise
6756
"""
68-
_validate_input_str(input_str)
6957
for i, c in enumerate(input_str):
7058
if c in input_str[i+1:]:
7159
return False
@@ -76,12 +64,10 @@ class TestIsUniqueFunction(unittest.TestCase):
7664
def _run_tests(self, f: Callable[[str], None]) -> None:
7765
for case in ["techqueria", "bobby", "california"]:
7866
self.assertFalse(f(case), msg=case)
79-
for case in ["tacos", "swag", "orbit", "e"]:
67+
for case in ["tacos", "swag", "orbit", "e", ""]:
8068
self.assertTrue(f(case), msg=case)
8169
with self.assertRaises(TypeError):
8270
f(8)
83-
with self.assertRaises(ValueError):
84-
f("")
8571

8672
def test_is_unique(self):
8773
self._run_tests(is_unique)

0 commit comments

Comments
 (0)