Build a Hash Table - Build a Hash Table

Tell us what’s happening:

Cant seem to get test 11 right, can anybody help me?

Your code so far

class HashTable:
    def __init__(self):
        self.collection = {}
    def hash(self, key):
        if not isinstance(key, str):
            raise ValueError('Key must be a string')
        hash_value = 0
        for char in key:
            hash_value += ord(char)
        return hash_value
    def add(self, key, value):
       hash_value = self.hash(key)
       if hash_value not in self.collection:
           self.collection[hash_value] = {}
           self.collection[hash_value][key] = value
    def remove(self, key):
        key_hash = self.hash(key)
        if key_hash in self.collection:
            if key in self.collection[key_hash]:
                del self.collection[key_hash][key]
        return
    def lookup(self, key):
        hash_value = self.hash(key)
        if hash_value in self.collection and key in self.collection[hash_value][key]:
            return self.collection[hash_value][key]
        return None

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0

Challenge Information:

Build a Hash Table - Build a Hash Table

Have you tested adding entries with identical hashes into your table and observing the structure of your table?

I don’t see any print() statements at all here, so it looks like you haven’t done any testing?

If multiple keys produce the same hash value, their key-value pairs should be stored in the existing nested dictionary under the same hash value.

I’m not able to add two keys that hash to the same value.

hashtable.add("test", 5)
>>{448: {'test': 5}}
hashtable.add("ttes", 6)
>>{448: {'test': 5}}