LEARN COMPLEX DATA STRUCTURES Blossom

hi can someone please help tell me why i am getting an attribute error from my HashMap??

from linked_list import Node, LinkedList
from blossom_lib import flower_definitions



class HashMap:
  def __init__(self, array_size):
    self.array_size = array_size
    self.array = [LinkedList() for value in range(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 item 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 = self.array[array_index]

      for item in list_at_index:
        if key == item[0]:
          return item[1]

        else: return None  



      return



blossom = HashMap(len(flower_definitions))

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

print(blossom.retrieve('daisy'))  

ERROR message:

Traceback (most recent call last):
  File "script.py", line 60, in <module>
    blossom.assign(flower[0], flower[1])
AttributeError: 'HashMap' object has no attribute 'assign'

https://www.codecademy.com/paths/computer-science/tracks/complex-data-structures/modules/cspath-hash-maps/projects/blossom

Hi!
Thanks for sharing your code.

The error is as it says, your HashMap object doesn’t have an attribute assign. So if you run dir(HashMap), you won’t see attribute, but you will see a directory of possible attributes you can call on.

In simpler terms, the action you want to do (to assign), is somewhere else, or has to be done by a different method.

You may want to see the HashMap object in your directory to further understand its inner workings for clues.

1 Like

oh i see! i thought that this line of code states that i Do have an assign attribute

def assign(self, key, value):
      array_index = self.compress(self.hash(key))  
      payload = Node([key, value])

You should! (it might be some indentation mistake or something that’s making your class not register it). This is something you should check…

I was just highlighting the troubleshooting process :slight_smile:

For example, when you run dir(blossom), if it’s not giving you the assign method as an option, and you’re sure you put it in, it means that something about how you put it in is not registering.

2 Likes