-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlossom app (Hash)
More file actions
36 lines (32 loc) · 1.04 KB
/
Blossom app (Hash)
File metadata and controls
36 lines (32 loc) · 1.04 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
from linked_list import Node, LinkedList
from blossom_lib import flower_definitions
class HashMap:
def __init__(self,size):
self.array_size = size
self.array = [LinkedList() for item in range(self.array_size)]
def hash(self,key):
key_bytes = key.encode()
hash_code = sum(key_bytes)
return hash_code
def compress(self,hash_code):
return hash_code % self.array_size
def assign(self,key,value):
array_index = self.compress(self.hash(key))
payload = Node([key, value])
list_at_array = self.array[array_index]
for item in list_at_array:
if key == item[0]:
item[1] = value
list_at_array.insert(payload)
def retrieve(self, key):
array_index = self.compress(self.hash(key))
list_at_index = self.array[array_index]
for item in list_at_index:
if item[0] == key:
return item[1]
else:
return None
blossom = HashMap(len(flower_definitions))
for flower in flower_definitions:
blossom.assign(flower[0],flower[1])
print(blossom.retrieve('daisy'))