Blossom Project

2 posts were split to a new topic: Blossom Project: NameError

Hi, I still don’t understand why the code is returning None for some flowers…

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 item in range(self.array_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 item in list_at_array:
# item[0] is the key and item[1] is the value
if item[0] == key:
item[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 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(‘daisy’))
print(blossom.retrieve(‘rose’))
print(blossom.retrieve(‘wisteria’))
print(blossom.retrieve(‘morning glory’))
print(blossom.retrieve(‘lavender’))
print(blossom.retrieve(‘carnation’))
print(blossom.retrieve(‘magnolia’))
print(blossom.retrieve(‘sunflower’))
print(blossom.retrieve(‘periwinkle’))

# innocence
# None
# None
# None
# devotion
# memories
# dignity
# longevity
# new friendship

Welcome to the forums!

Please format your code according to the guidelines in the following topic. Indentation is important in Python and we need to see your indentation in order to tell what went wrong.

Thanks, Victoria. Here’s the formatted code:

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 item in range(self.array_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 item in list_at_array:
      # item[0] is the key and item[1] is the value
      if item[0] == key:
        item[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 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('daisy'))
print(blossom.retrieve('rose'))
print(blossom.retrieve('wisteria'))
print(blossom.retrieve('morning glory'))
print(blossom.retrieve('lavender'))
print(blossom.retrieve('carnation'))
print(blossom.retrieve('magnolia'))
print(blossom.retrieve('sunflower'))
print(blossom.retrieve('periwinkle'))

# innocence
# None
# None
# None
# devotion
# memories
# dignity
# longevity
# new friendship

Take a look at your retrieve function, and more specifically, your for loop. We want to return the value of the corresponding key in the linked list if the key exists. Otherwise, we want to return None. Make sure you are checking all of the keys in the linked list against the key passed to the function as an argument.

Hint

Since a function will be exited after a return statement is executed, your for loop causes the function to either return the correct value if it belongs to the head node of the linked list, or None if the value does not belong to the head node. The loop doesn’t check if the other nodes in the linked list have the same key as the one passed to the function as an argument since return None will be executed on the first iteration of the loop if return item[1] is not.

1 Like

Thanks again @victoria_dr. I corrected the indentation again and now it prints the values correctly:

# innocence
# love
# good luck
# unrequited love
# devotion
# memories
# dignity
# longevity
# new friendship

1 Like

Hi there,

In this blossom project, the additional linked_list script has a iter method:

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()

I have some difficulties to understand the why and when this method is use and also the yield keyword. I checked over the internet to get some info, but I don’t really understand this situation. Could someone give me some explanations please?

######################################################################
my bad, I found my answer in a previous post from @appylpye . Great explanation, thanks so much.
######################################################################

click on the folder icon on the top left corner where you write the code in codecademy

I confused on how the linked list method works.
In:


self.array = [LinkedList() for item in range(self.array_size)]

I understand that it is going to be a list of linked list objects, but it would help me learn by being able to iterate through the entire map after this code:

blossom = HashMap(len(flower_definitions))
for flower in flower_definitions:
  blossom.assign(flower[0], flower[1])

Using a for loop that iterates through blossom instance does not work so how to you access the iter method from LinkedList from the blossom instance? I’m not sure I’m making sense. But does anyone have an idea how to make a class method of HashMap that can use iter from the LinkedList class? Basically I just want to print out the whole map after instantiation and adding the flowers to hashmap.

I had a question here, but I figured it out by looking at previous posts. :slightly_smiling_face: