-
-
Save codecademydev/4dce37e21a8ef39979ea937747133cb7 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 | |
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) | |
def __iter__(self): | |
current_node = self.head_node | |
while(current_node): | |
yield current_node.get_value() |
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): | |
hash_code = self.hash(key) | |
array_index = self.compress(hash_code) | |
payload = Node([key, value]) | |
list_at_array = self.array[array_index] | |
for node in list_at_array: | |
if node[0] == key: | |
node[1] = value | |
return | |
list_at_array.insert(payload) | |
def retrieve(self, key): | |
hash_code = self.hash(key) | |
array_index = self.compress(hash_code) | |
list_at_index = self.array[array_index] | |
print(key) | |
for node in list_at_index: | |
if node[0] == key: | |
print(node[0], node[1]) | |
return node[1] | |
return None | |
print('Hey') | |
blossom = HashMap(len(flower_definitions)) | |
for flower in flower_definitions: | |
print(flower) | |
blossom.assign(flower[0], flower[1]) | |
print(blossom.retrieve('daisy')) | |
print(blossom.retrieve('begonia')) | |
print("hey") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment