0

I have this hash table and i want to know how to add a value to its values if the keys are the same. with what ive tried ive gotten a cannot find reference append in None

Heres how the table looks:

self._example = [['hello',[1,2,3]],['world',[4,5,6]], [random,[7,8,9]]]

here is the code ive tried:

    def add(self,key,value):
        i = self._hash(key)
        if self._example[i] != None:
            while true: 
                 if self._example[i][0] == key:
                      self._example[i][1].append(value)
                      break
Squilliam
  • 31
  • 5
  • 1
    Does this answer your question? [How to add multiple values to a dictionary key?](https://stackoverflow.com/questions/20585920/how-to-add-multiple-values-to-a-dictionary-key) – Garrett Hyde Dec 03 '22 at 00:53
  • A few problems are evident: - your code doesn't handle inserting a value for a new hash/key - your code also doesn't seem to handle multiple keys giving the same hash result, as you require _example[i][0] == key. - self._example[i] could result in an IndexError. – nigh_anxiety Dec 03 '22 at 01:12
  • what does your hash function look like? – nigh_anxiety Dec 03 '22 at 01:13
  • it just gets the modulus of ord of every element in the string added together with the size. I have in other parts of the code on how to get the new hash/key and they work its just this part thats giving me some trouble – Squilliam Dec 03 '22 at 01:15
  • How are you handling hash collisions? You're currently storing the key as part of each entry, and then requiring the key to be an exact match. Based on the description of your hash function, anagrams will result in the same result from self._hash(key), so entries such as "melon" and "lemon" will collide. Whether you want anagrams to actually share the same hash value and position in the table, or you want to use some sort of open addressing or chaining (i.e. a linked list) to handle collisions changes the answer, and you may need to change the current table structure to accomodate. – nigh_anxiety Dec 03 '22 at 08:35

0 Answers0