-
-
Save codecademydev/4332199a0b20c03b72726bf0e93b4413 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 i in range(size)] | |
def hash(self, key): | |
hash_code = sum(key.encode()) | |
return hash_code | |
def compressor(self, hash_code): | |
return hash_code % self.array_size | |
def assign(self, key, value): | |
array_index = self.compressor(self.hash(key)) | |
payload = Node([key,value]) | |
list_at_array = self.array[array_index] | |
for item in list_at_array: | |
if item[0] == key: | |
item[1] = value | |
return | |
list_at_array.insert(payload) | |
def retrieve(self, key): | |
array_index = self.compressor(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]) | |
print(blossom.retrieve('rose')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment