-
-
Save codecademydev/e8f13e3548016abe4c2da64eccb7ca5b 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
flower_definitions = [['begonia', 'cautiousness'], ['chrysanthemum', 'cheerfulness'], ['carnation', 'memories'], ['daisy', 'innocence'], ['hyacinth', 'playfulness'], ['lavender', 'devotion'], ['magnolia', 'dignity'], ['morning glory', 'unrequited love'], ['periwinkle', 'new friendship'], ['poppy', 'rest'], ['rose', 'love'], ['snapdragon', 'grace'], ['sunflower', 'longevity'], ['wisteria', 'good luck']] |
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): | |
return sum(key.encode()) | |
def compress(self, hash_code): | |
return self.array_size % hash_code | |
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 key == i[0]: | |
i[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 i in list_at_index: | |
if key == i[0]: | |
return i[1] | |
return None | |
blossom = HashMap(len(flower_definitions)) | |
for flower in flower_definitions: | |
blossom.assign(flower[0], flower[1]) | |
print(blossom.retrieve('daisy')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment