我想在Python中編寫一個函數,它會將字符串添加到哈希表中,並且在不導入數學的情況下使用二次探測解決任何衝突。在Python中使用二次探測的字符串哈希
def addString(string, hashTable):
collisions = 0
stop = False
slot = (hashString(string, len(hashTable)))
while not stop:
if hashTable[slot] == None:
hashTable[slot] = string
stop = True
else:
slot = slot + (collisions**2)%len(hashTable)
collisions = collisions + 1
print('collisions: ', collisions)
我的問題是,我不斷收到IndexError:列表索引超出範圍,我敢肯定問題出在else塊不過,我似乎無法找到它的解決方案。任何幫助表示感謝,謝謝。
什麼是散列串? –
發生異常的線是什麼? –
你在哪裏得到IndexError?我看到你做的唯一索引是'hashTable [slot]'。 – acdr