-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash_table.py
82 lines (70 loc) · 2.03 KB
/
hash_table.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class HashTable:
def __init__(self):
"""
Initialize a hash table with a default size of 10.
"""
self.size = 10
self.table = [[] for _ in range(self.size)]
def _hash_function(self, key):
"""
Hashes the given key and returns the index in the hash table.
"""
return hash(key) % self.size
def insert(self, key, value):
"""
Inserts a key-value pair into the hash table.
"""
index = self._hash_function(key)
self.table[index].append((key, value))
def get(self, key):
"""
Retrieves the value associated with the given key from the hash table.
Raises a KeyError if the key is not found.
"""
index = self._hash_function(key)
for k, v in self.table[index]:
if k == key:
return v
raise KeyError(f"Key '{key}' not found in the hash table.")
def remove(self, key):
"""
Removes the key-value pair associated with the given key from the hash table.
Raises a KeyError if the key is not found.
"""
index = self._hash_function(key)
for i, (k, v) in enumerate(self.table[index]):
if k == key:
del self.table[index][i]
return
raise KeyError(f"Key '{key}' not found in the hash table.")
hash_1 = HashTable()
hash_1.insert("a", 1)
hash_1.insert("b", 2)
hash_1.insert("c", 3)
hash_1.insert("d", 4)
hash_1.insert("e", 5)
hash_1.insert("f", 6)
hash_1.insert("g", 7)
hash_1.insert("h", 8)
hash_1.insert("i", 9)
hash_1.insert("j", 10)
hash_1.insert("k", 11)
hash_1.insert("l", 12)
hash_1.insert("m", 13)
hash_1.insert("n", 14)
hash_1.insert("o", 15)
hash_1.insert("p", 16)
hash_1.insert("q", 17)
print(hash_1.get("a"))
print(hash_1.get("b"))
print(hash_1.get("c"))
hash_1.remove("a")
hash_1.remove("b")
hash_1.remove("c")
print(hash_1.get("a"))
print(hash_1.get("b"))
print(hash_1.get("c"))
hash_1.insert("a", 1)
hash_1.insert("b", 2)
hash_1.insert("c", 3)
print(hash_1.get("a"))