Blossom project help


#1

I finished the blossom project, but it won’t print the retrieved value for “daisy”. Can anyone see where I may have messed up?

from blossom_lib import flower_definitions

class HashMap:
  def __init__(self, size):
    self.array_size = size
    
    self.array = [LinkedList() for number in range(self.array_size)]
    
  def hash(self, key):
    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))
    [key, value] = self.array(array_index) 
    payload = Node([key, value])
    list_at_array = self.array[array_index] 
    for i in list_at_array:
      if key == item[0]:
        item[1] = value 
      if key != item[0]:
        list_at_array.insert(payload)
        
 
  def retrieve(self, key):
    array_index = self.compress(self.hash(key))
    list_at_index = LinkedList(self.array)
    for item in list_at_index:
      if key == item[0]:
        return item[1]
      if key != item[0]:
        return None
 
    blossom = HashMap(len(flower_definitions))
    for flower in flower_definitions: 
      blossom.assign(flower[0], flower[1])
    print(blossom.retrieve('daisy'))

#2

Hi @bbalagia,

You have all these statements indented by four or more spaces, making them part of the retrieve method of HashMap:

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

They should not be part of that method, nor be part of HashMap at all. Remove four spaces of indentation from each, which would place them in the global scope.

EDIT: Added the following on March 16, 2019:

There are some additional problems, including the following:

LinkedList and Node need to be defined via this import statement:

from linked_list import Node, LinkedList

The hash method of HashMap computes a value but does not return it.

Within the for loops of the assign and retrieve methods of HashMap, the second if blocks should execute only if the for loops complete all of their iterations without finding a match between key and item[0]. Therefore, those if blocks should not be contained within the for loops.

Please let us know whether or not you are able to resolve these and any other remaining problems with this project.