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