-
Notifications
You must be signed in to change notification settings - Fork 6
/
12-dictionary.py
60 lines (54 loc) · 1.51 KB
/
12-dictionary.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
class Dictionary:
def __init__(self, table_size, prime):
self.prime = prime
self.item_count = 0
self.table_size = table_size
self.array = [None] * table_size
def hash(self, key):
index = 0
a = 2692
b = 8523
for i in range(len(key)):
index = (a * index + ord(key[i])) % self.table_size
a = a * b % (self.table_size-1)
return index
def is_full(self):
return self.item_count >= self.table_size
def insert(self, key, value):
index = self.hash(key)
if self.is_full():
found = False
end = (index - 1) % self.table_size
while index != end:
if self.array[index][0] == key:
self.array[index] = [key, value]
found = True
break
index = (index + 1) % self.table_size
if not found:
raise Exception("Dictionary is full!")
else:
while not self.is_full():
if self.array[index] != None and self.array[index][0] == key:
self.array[index] = [key, value]
break
if self.array[index] == None:
self.array[index] = [key, value]
self.item_count += 1
break
index = (index + 1) % self.table_size
def search(self, key):
index = self.hash(key)
if self.array[index][0] == key:
return self.array[index][1]
else:
end = (index - 1) % self.table_size
if self.array[end][0] == key:
return self.array[end][1]
index = (index + 1) % self.table_size
while index != end:
if self.array[index][0] == key:
return self.array[index][1]
else:
index = (index + 1) % self.table_size
raise KeyError(str(key) + " not found!")