-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.py
46 lines (41 loc) · 1.35 KB
/
HashTable.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
class HashItem:
def __init__(self, key, value):
self.key = key
self.value = value
class HashTable:
def __init__(self, size=256):
self.size = size
self.slots = [None] * self.size
self.count = 0
def _hash(self, key):
return abs(hash(key)) % self.size
def __setitem__(self, key, value):
if self.count < self.size:
item = HashItem(key, value)
h = self._hash(key)
while self.slots[h] is not None:
if self.slots[h].key is key:
break
h = (h + 1) % self.size
if self.slots[h] is None:
self.count += 1
self.slots[h] = item
def __getitem__(self, key):
h = self._hash(key)
while self.slots[h] is not None:
if self.slots[h].key is key:
return self.slots[h].value
h = (h + 1) % self.size
return None
if __name__ == "__main__" :
ht = HashTable()
print(ht.size, hash("good"), ht._hash("good"))
ht["good"] = "eggs"
ht["better"] = "ham"
ht["best"] = "spam"
ht["ad"] = "do not"
ht["ga"] = "collide"
for key in ("good", "better", "best", "worst", "ad", "ga"):
v = ht[key]
print(v)
print("The number of elements is: {}".format(ht.count))