-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.py
More file actions
47 lines (42 loc) · 1.69 KB
/
HashMap.py
File metadata and controls
47 lines (42 loc) · 1.69 KB
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
class HashMap:
def __init__(self, array_size):
self.array_size = array_size
self.array = [None for num in range(self.array_size)]
def hash(self, key, collisions=0):
key_bytes = key.encode()
hash_code = sum(key_bytes)
return hash_code + collisions
def compressor(self, hashcode):
return hashcode % self.array_size
def assign(self, key, value):
index = self.compressor(self.hash(key))
current_value = self.array[index]
if (current_value is None) or (current_value[1] == value):
self.array[index] = [key, value]
elif current_value != key:
collisions = 1
while current_value != key:
new_index = self.compressor(self.hash(key, collisions))
current_value = self.array[new_index]
if (current_value is None) or (current_value[1] == value):
self.array[new_index] = [key, value]
return
else:
collisions += 1
return
def retrieve(self, key):
current_value = self.array[self.compressor(self.hash(key))]
if current_value is None:
return None
elif current_value[0] == key:
return current_value[1]
elif current_value[0] != key:
collisions = 1
while current_value[0] != key:
current_value = self.array[self.compressor(self.hash(key, collisions))]
if current_value is None:
return None
elif current_value[0] == current_value:
return current_value[1]
else:
collisions += 1