-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
49 lines (32 loc) · 1.09 KB
/
script.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
from linked_list import Node, LinkedList
class HashMap:
def __init__(self, size):
self.array_size = size
self.array = [LinkedList() for number in range(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 i in list_at_array:
if i[0] == key:
i[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 i in list_at_index:
if i[0] == key:
return i[1]
return None
from blossom_lib import flower_definitions
blossom = HashMap(len(flower_definitions))
for i in flower_definitions:
blossom.assign(i[0], i[1])
print(blossom.retrieve('daisy'))
blossom.assign('violet', 'fidelity')
print(blossom.retrieve('violet'))