Skip to content

Commit 221d740

Browse files
Update
1 parent 2aa1327 commit 221d740

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

Chapter08/hashtable.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(self):
1010
self.slots = [None for i in range(self.size)]
1111
self.count = 0
1212
self.MAXLOADFACTOR = 0.65
13-
13+
self.prime_num = 5
1414

1515
def check_growth(self):
1616
loadfactor = self.count / self.size
@@ -97,7 +97,7 @@ def h2(self, key):
9797
return hv
9898

9999

100-
def put_doubleHashing(self, key, value):
100+
def put_double_hashing(self, key, value):
101101
item = HashItem(key, value)
102102
h = self._hash(key)
103103
j = 1
@@ -111,7 +111,7 @@ def put_doubleHashing(self, key, value):
111111
self.slots[h] = item
112112
self.check_growth()
113113

114-
def get_doubleHashing(self, key):
114+
def get_double_hashing(self, key):
115115
h = self._hash(key)
116116
j = 1
117117
while self.slots[h] != None:
@@ -129,20 +129,44 @@ def __setitem__(self, key, value):
129129

130130
def __getitem__(self, key):
131131
return self.get(key)
132+
133+
134+
135+
ht = HashTable()
136+
ht.put_quadratic(“good”, “eggs”)
137+
ht.put_quadratic(“ad”, “packt”)
138+
ht.put_quadratic(“ga”, “books”)
139+
v = ht.get_quadratic(“ga”)
140+
print(v)
132141

133142

134143
ht = HashTable()
135144
ht.put("good", "eggs")
136145
ht.put("better", "ham")
137146
ht.put("best", "spam")
138147
ht.put("ad", "do not")
139-
ht.put("ga", "collide")
148+
ht.put("ga", "collide")
149+
ht.put(“awd”, “do not”)
150+
ht.put(“add”, “do not”)
151+
ht.checkGrow()
140152

141153
for key in ("good", "better", "best", "worst", "ad", "ga"):
142154
v = ht.get(key)
143155
print(v)
144156

145-
157+
158+
ht = HashTable()
159+
ht.put_double_hashing(“good”, “eggs”)
160+
ht.put_double_hashing(“better”, “spam”)
161+
ht.put_double_hashing(“best”, “cool”)
162+
ht.put_double_hashing(“ad”, “donot”)
163+
ht.put_double_hashing(“ga”, “collide”)
164+
ht.put_double_hashing(“awd”, “hello”)
165+
ht.put_double_hashing(“addition”, “ok”)
166+
for key in (“good”, “better”, “best”, “worst”, “ad”, “ga”):
167+
v = ht.get_double_hashing(key)
168+
print(v)
169+
print(“The number of elements is: {}”.format(ht.count))
146170

147171

148172
ht = HashTable()

0 commit comments

Comments
 (0)