-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw04.py
66 lines (53 loc) · 1.61 KB
/
hw04.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def smallest(plist):
if len(plist) == 1:
return int(plist[0])
if plist[0] < plist[1]:
plist[1] = plist[0]
return smallest(plist[1:])
def linearSearchValueIndexEqual(plist):
if not plist:
return []
final = []
count = 0
for i in plist:
if i == count:
final.append(i)
count += 1
return final
def binarySearchValueIndexEqual(plist):
if not plist:
return []
final = []
for index in range(len(plist)):
left = 0
right = len(plist) - 1
while left <= right:
middle = (right - left) // 2 + left
if plist[middle] == index:
if index == plist[index]:
final.append(middle)
break
elif index < plist[middle]:
right = middle - 1
else:
left = middle + 1
return final
class Hashtable:
def __init__(self, items):
self.bucketsize = len(items)
self.buckets = [[] for i in range(self.bucketsize)]
self.assign_buckets(items)
def assign_buckets(self, items):
for element in items:
hash_code = hash(element)
index = hash_code % self.bucketsize
self.buckets[index].append((items))
def anonymousLetter(book, letter):
group = (book, letter)
hashtable = Hashtable(group)
for ch in letter:
if ch not in book:
return False
elif letter.count(ch) > book.count(ch):
return False
return True