-
-
Save codecademydev/4e620da0e7505461470226c59cbe94f2 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
class Node: | |
def __init__(self, value): | |
self.value = value | |
self.next_node = None | |
def get_value(self): | |
return self.value | |
def get_next_node(self): | |
return self.next_node | |
def set_next_node(self, next_node): | |
self.next_node = next_node | |
class LinkedList: | |
def __init__(self, head_node=None): | |
self.head_node = head_node | |
def insert(self, new_node): | |
current_node = self.head_node | |
if not current_node: | |
self.head_node = new_node | |
while(current_node): | |
next_node = current_node.get_next_node() | |
if not next_node: | |
current_node.set_next_node(new_node) | |
current_node = next_node | |
def __iter__(self): | |
current_node = self.head_node | |
while(current_node): | |
yield current_node.get_value() | |
current_node = current_node.get_next_node() |
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(self.array_size)] | |
def hash(self, key): | |
hash_code = sum(key.encode()) | |
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 pair in list_at_array: | |
if pair[0] == key: | |
pair[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] | |
if list_at_index != None: | |
for pair in list_at_index: | |
if pair[0] == key: | |
return pair[1] | |
else: | |
return None | |
else: | |
return None | |
blossom = HashMap(len(flower_definitions)) | |
for item in flower_definitions: | |
blossom.assign(item[0], item[1]) | |
print(blossom.retrieve('daisy')) | |
print(blossom.retrieve('chrysanthemum')) | |
blossom.assign("fern", "asfg") | |
print(blossom.retrieve('asfg')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment