-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution blossom
37 lines (30 loc) · 1 KB
/
solution blossom
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
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):
return sum(key.encode())
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:
# item[0] is the key and item[1] is the value
if item[0] == key:
item[1] = value
return
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]
return None
blossom = HashMap(len(flower_definitions))
for flower in flower_definitions:
blossom.assign(flower[0], flower[1])