class MyHashTable:
def __init__(self, capacity):
self.capacity = capacity
self.slots = [None] * self.capacity
def __str__(self):
return str(self.slots)
def __len__(self):
count = 0
for i in self.slots:
if i != None:
count += 1
return count
def hash_function(self, key):
slot = key % len(self.slots)
if key in self.slots:
return slot
elif (not key in self.slots) and len(self.slots) == self.capacity:
return slot
else:
for i in self.slots:
count = 0
if i == None:
return count
count += 1
def insert(self, key):
print(len(self.slots)) #Why does this show an output of 2?
if key in self.slots:
return -2
elif (not key in self.slots) and (len(self.slots) != self.capacity): #Now this cant execute
num = hash_function(key)
self.slots[num] = key
return num
elif (not key in self.slots) and len(self.slots) == self.capacity:
return -1
Im интересно, почему комментируемая часть выше в insert(self, key)
операторах печати дает (2) вместо (0). Заявление Элиф под обыкновение выполнять, так как его дает результат (2) вместо (0)код отслеживания и логические ошибки
Функция вызов
x = MyHashTable(2)
print(len(x))
Если дать: 0
Вы не должны сравнивать с 'None' с' == '. Вместо этого вы должны проверить, например, 'if i is None 'или' if i is not None' – donkopotamus