forked from WilliamQLiu/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings_unique_chars.py
More file actions
35 lines (24 loc) · 834 Bytes
/
strings_unique_chars.py
File metadata and controls
35 lines (24 loc) · 834 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
#! /usr/bin/env python
""" Does a string contain all unique characters? """
import unittest
class UniqueTest(unittest.TestCase):
def test_bf_no_duplicates(self, myinput='abcde'):
result = brute_force(myinput)
self.assertEqual(result, True)
def test_bf_duplicates(self, myinput='aabcde'):
result = brute_force(myinput)
self.assertEqual(result, False)
def brute_force(myinput):
"""
Get input string, return True if string has all unique characters,
otherwise return False if a duplicate character exists
"""
for a in range(len(myinput)-1):
for b in range(a+1, len(myinput)-1):
if myinput[a] == myinput[b]:
return False
else:
pass
return True
if __name__ == '__main__':
unittest.main()