-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy path#8_isUnique.py
More file actions
39 lines (30 loc) · 929 Bytes
/
#8_isUnique.py
File metadata and controls
39 lines (30 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# O(N)
import unittest
def unique(string):
# Assuming character set is ASCII (128 characters)
if len(string) > 128:
return False
char_set = {}
for char in string:
if char in char_set:
# Char already found in string
return False
char_set[char] = True
return True
class Test(unittest.TestCase):
# unittest provides a base class, TestCase,
# which may be used to create new test cases.
dataT = [('abcd'), ('s4fad'), ('')]
dataF = [('23ds2'), ('hb 627jh=j ()')]
def test_unique(self):
# true check
for test_string in self.dataT:
actual = unique(test_string)
self.assertTrue(actual)
# false check
for test_string in self.dataF:
actual = unique(test_string)
self.assertFalse(actual)
if __name__ == "__main__":
unittest.main()
# print(unique('abcd'))