Build a Hash Table - Build a Hash Table: Add Method

Tell us what’s happening:

User Story for the Add Method: 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 don’t understand this? I should have a dictionary of dictionary sets(?) but how would you have more than 1 value per key when every key has to be unique? And my little test obviously didn’t work, which I expected.

I also had a question about intializing the empty dictionary and why it’s that way to pass #2?

Your code so far

class HashTable:
    def __init__(self, collection = {}):
        collection = {} #why did I need to add this to pass the validation?
        self.collection = collection
    
    def __str__(self):
        return f"{self.collection.items()}"
    
    def hash(self, string):
        hash_value = 0
        for i in string:
            hash_value += ord(i)
        return hash_value
    
    def add(self, key, value):
        hash_value = self.hash(key)
        temp = self.collection[hash_value] = {hash_value: value}
        return temp

my_table = HashTable()
print(my_table.add("ben", "ben"))
print(my_table.add("neb", "neb"))
print(my_table)

Your browser information:

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

Challenge Information:

Build a Hash Table - Build a Hash Table

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-hash-table/67ed03ac474c48692f41749e.md at main · freeCodeCamp/freeCodeCamp · GitHub

you need to nest a dictionary at a certain hash value, and a dictionary can have multiple key/value pairs

Yeah, I Google’d the answers to my questions as I worked through it.

According to Google, when I create an instance of the empty dictionary, without explicitly creating an empty dictionary each time, it would blend the dictionaries together if I had created more than one hash table from that class. It recommended setting the default value for the collection argument to None as better practice(?).

When Google walked me through why the values were overwriting each in the dictionary other I understood why others were creating empty dictionaries in the add method.

Didn’t help I misunderstood the directions at the very start (right after posting my question I realized it) thinking it wanted me to store {hash-value: value} and not {hash-value: {key: value}}.

yeah, we noticed we missed that behaviour of default arguments, and I am working right now of adding it to the curriculum

You all are amazing!

Tangent: my dad and uncle, who are 80, are baffled at my desire to leave my current job making almost 100k with decent benefits. And I try to explain that there is no reason for me to stay in a place I hate when there is nearly anything I could want to learn at my fingertips. The point: that’s thanks to you and everyone here, and the baffling amount of work this website must take. So, thanks!

you are doing great! just be ready it takes time, do not leave a job without an other lined up or enough savings to live on for a while