u7fix2
This commit is contained in:
parent
a02170bbd1
commit
a7800a5027
@ -1,4 +1,5 @@
|
||||
"""Hash tabuľka s lineárnym sondovaním a reťazcovými kľúčmi."""
|
||||
from operator import index
|
||||
from unittest import result
|
||||
|
||||
|
||||
@ -20,6 +21,8 @@ class HashTable:
|
||||
|
||||
def hash(self, key):
|
||||
"""Vráť polynomiálny hash kľúča modulo aktuálna kapacita."""
|
||||
if not isinstance(key, str):
|
||||
raise TypeError("kluc ma byt retaz")
|
||||
result = 0
|
||||
for char in str(key):
|
||||
result = result * 31 + ord(char)
|
||||
@ -27,12 +30,22 @@ class HashTable:
|
||||
|
||||
def probe(self, key):
|
||||
"""Vráť index kľúča alebo miesta na vloženie bez zmeny tabuľky."""
|
||||
if not isinstance(key, str):
|
||||
raise TypeError("kluc ma byt retaz")
|
||||
i = 0
|
||||
del_slot = None
|
||||
while i < self.capacity:
|
||||
index = (self.hash(key) + i) % self.capacity
|
||||
if self.table[index] is self.empty:
|
||||
if del_slot is None:
|
||||
return index
|
||||
elif self.table[index] is not self.deleted and self.table[index][0] == key:
|
||||
else:
|
||||
return del_slot
|
||||
elif self.table[index] is self.deleted:
|
||||
if del_slot is None:
|
||||
del_slot = index
|
||||
|
||||
elif self.table[index][0] == key:
|
||||
return index
|
||||
i += 1
|
||||
raise KeyError(key)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user