-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash_table.py
34 lines (25 loc) · 1006 Bytes
/
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
class HashTable:
def __init__(self, size):
self.size = size
self.table = [None] * size
def hash_function(self, key):
# A vary simple hash function (modulo)
return hash(key) % self.size
def insert(self, key, value):
index = self.hash_function(key)
# Linear probing for collision resolution
while self.table[index] is not None:
index = (index + 1) % self.size
self.table[index] = (key, value)
def get(self, key):
index = self.hash_function(key)
original_index = index # Keep track of where we started (prevent infinite loop)
while self.table[index] is not None:
if self.table[index][0] == key:
return self.table[index][1]
index = (index + 1) % self.size
if index == original_index: # Means we've looped through the whole table
return None
return None
def __str__(self):
return str(self.table)