Tell us what’s happening:
Part 11 and 17 isn’t getting solved. I know it is saying not to delete the entire dictionary but I don’t understand how is it deleting the entire if I am only deleting at a certain key and also part 17, i dont know what to do
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_sum = 0
for char in key:
hash_sum += ord(char)
return hash_sum
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
else:
self.collection[hash_value][key] = value
return self.collection[hash_value][key]
def remove(self,key):
hash_value = hash(key)
if hash_value in self.collection:
if key in self.collection[hash_value]:
del self.collection[hash_value][key]
return
def lookup(self,key):
hash_value = self.hash(key)
if hash_value in self.collection and key in self.collection[hash_value]:
return self.collection[hash_value][key]
return None
print(HashTable().add('dear', 'friend'))
print(HashTable().add('read', 'book'))
print(HashTable().hash('read'))
print(HashTable().lookup('dear'))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Challenge Information:
Build a Hash Table - Build a Hash Table