Blossom - Query with spaces in strings

Hi guys,

strange query been able to complete the task and can add/assign new flowers as needed, however it seems that either my assign and/or retrieve method fails whenever there is a " "(empty inside a string).

It works as expected when there arent any empty spaces within the string name of.
From what I understand an empty space inside a string should/would be treated as if it’s any other character. any help/feedback would be much appreciated :slight_smile:

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

link to project:
https://www.codecademy.com/courses/complex-data-structures/projects/blossom

my 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(size)]
  
  def hash(self, key):
    hash_code = sum(key.encode())
    return hash_code
  
  def compress (self, hash_code):
    array_index = hash_code % self.array_size
    return array_index
  
  def assign(self, key, value):
    key_hash_code = self.hash(key)
    array_index = self.compress(key_hash_code)
    payload = Node([key, value])
    list_at_array = self.array[array_index]
    for item in list_at_array:
      if key == item[0]:
        item[1] = value
    list_at_array.insert(payload)
        

  def retrieve(self, key):
    key_hash_code = self.hash(key)
    array_index = self.compress(key_hash_code)
    list_at_index = self.array[array_index]
    for item in list_at_index:
      if key == item[0]:
        return item[1]
      else:
        return None
   
blossom = HashMap(len(flower_definitions))


#lossom.assign("daisy", "testing ■■■■ you")
for flowers in flower_definitions:
  blossom.assign(flowers[0], flowers[1])

print(blossom.retrieve("daisy"))

blossom.assign("venusflytrap", "testing1")
print(blossom.retrieve("venusflytrap"))
blossom.assign("venus fly trap", "tesing2")
print(blossom.retrieve("venus fly trap"))
blossom.assign("lilac", "testing3")
print(blossom.retrieve("lilac"))

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.