From e2afd1cabda0a88fba6676dbbc660fc8e5bb3889 Mon Sep 17 00:00:00 2001 From: Patrik Tulach Date: Tue, 27 Oct 2026 13:54:15 +0100 Subject: [PATCH] u7 --- u7/hash_table.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/u7/hash_table.py b/u7/hash_table.py index 343a430..f22486a 100644 --- a/u7/hash_table.py +++ b/u7/hash_table.py @@ -1,13 +1,14 @@ """Hash tabuľka s lineárnym sondovaním a reťazcovými kľúčmi.""" -_DELETED = object() +_DELETED = object() # značka odstránenej položky + class HashTable: def __init__(self, capacity=10): if type(capacity) is not int: raise TypeError("Kapacita musí byť celé číslo.") if capacity <= 0: - raise ValueError("Kapacita musí byť kladné číslo.") + raise ValueError("Kapacita musí byť kladná.") self._slots = [None] * capacity self._size = 0 @@ -18,13 +19,14 @@ class HashTable: @staticmethod def _check_key(key): if not isinstance(key, str): - raise TypeError("Klúč musí byť typu string.") + raise TypeError("Kľúč musí byť reťazec.") def _is_active(self, index, key): slot = self._slots[index] return slot is not None and slot is not _DELETED and slot[0] == key def hash(self, key): + """Vráť polynomiálny hash kľúča modulo aktuálna kapacita.""" self._check_key(key) h = 0 for char in key: @@ -49,6 +51,10 @@ class HashTable: return first_deleted raise RuntimeError("Tabuľka je plná.") + def load_factor(self): + """Vráť podiel počtu aktívnych položiek a kapacity.""" + return len(self) / self.capacity + def _resize(self, new_capacity): old_slots = self._slots self._slots = [None] * new_capacity