-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion1_1.py
More file actions
41 lines (34 loc) · 1.03 KB
/
Question1_1.py
File metadata and controls
41 lines (34 loc) · 1.03 KB
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
40
41
__author__ = "Jonathan Mares"
# Implement an algorithm to determine if a string has all unique characters.
# What if you cannot use additional data structures?
#Using Hashtable
def uniqueChars(str):
chars = {}
for x in str:
try:
if chars[x]==True:
return False
except:
chars[x]=True
return True
#Using No additional data structures,
# this is not efficient tho (n^2)
# because calls find on every element, which is itself O(n)
def uniqueChars2(str):
for x in range (0,len(str)):
if str[x] in str[x+1:]:
return False
return True
testStrings =["Hello","Hhello","PPPP","aeoiu","abcdefghijklmnop"]
testBool =[False,False,False,True,True]
#returns true if all inputs are correct. Change to uniqueChars2 to test it.
def tester(strings,boolList):
boolgen = []
for str in strings:
boolgen.append(uniqueChars2(str))
if boolList == boolgen:
return True
else: return False
def main():
print(tester(testStrings,testBool))
main()