diff --git a/01_pole.py b/01_pole.py index 0ef2919..76b932c 100644 --- a/01_pole.py +++ b/01_pole.py @@ -8,27 +8,23 @@ def measure_time(operation, *args): return result, end - start def add_elements(array, elements): - # TODO: Přidat všechny prvky z "elements" do pole "array" - pass + array.extend(elements) def find_elements(array, elements): - # TODO: Najít všechny prvky z "elements" v poli "array" - pass + return [el for el in elements if el in array] if __name__ == "__main__": array = [] data = list(range(1, 100001)) random.shuffle(data) - # Přidávání po dávkách for i in range(0, len(data), 10000): batch = data[i:i+10000] _, duration = measure_time(add_elements, array, batch) print(f"Adding batch {i//10000 + 1}: {duration:.6f} s") - # Hledání po dávkách random.shuffle(data) for i in range(0, len(data), 10000): batch = data[i:i+10000] - _, duration = measure_time(find_elements, array, batch) - print(f"Finding batch {i//10000 + 1}: {duration:.6f} s") + found, duration = measure_time(find_elements, array, batch) + print(f"Finding batch {i//10000 + 1}: {duration:.6f} s, Found: {len(found)}") diff --git a/02_hashmap_chaining.py b/02_hashmap_chaining.py index fa3eba3..2ff7f33 100644 --- a/02_hashmap_chaining.py +++ b/02_hashmap_chaining.py @@ -7,18 +7,30 @@ def __init__(self, size): self.table = [[] for _ in range(size)] def hash_function(self, key): - # TODO: Implementovat hashovací funkci - pass + """Jednoduchá hashovací funkce.""" + return key % self.size def add(self, key, value): - # TODO: Přidat prvek s klíčem "key" a hodnotou "value" do hashmapy - pass + """Přidá prvek s klíčem `key` a hodnotou `value` do hashmapy.""" + index = self.hash_function(key) + # Pokud klíč již existuje, přepíšeme hodnotu + for i, (k, v) in enumerate(self.table[index]): + if k == key: + self.table[index][i] = (key, value) + return + # Jinak přidáme nový pár + self.table[index].append((key, value)) def find(self, key): - # TODO: Najít prvek s klíčem "key" v hashmapě a vrátit jeho hodnotu - pass + """Najde a vrátí hodnotu spojenou s klíčem `key`. Pokud klíč neexistuje, vrátí `None`.""" + index = self.hash_function(key) + for k, v in self.table[index]: + if k == key: + return v + return None def measure_time(operation, *args): + """Změří dobu trvání operace.""" start = time.time() result = operation(*args) end = time.time() diff --git a/03_hashmap_open_addressing.py b/03_hashmap_open_addressing.py index 95d0a4f..0323734 100644 --- a/03_hashmap_open_addressing.py +++ b/03_hashmap_open_addressing.py @@ -7,25 +7,53 @@ def __init__(self, size): self.table = [None] * size def hash_function(self, key): - # TODO: Implementovat hashovací funkci - pass + """Jednoduchá hashovací funkce.""" + return key % self.size def add(self, key, value): - # TODO: Přidat prvek s klíčem "key" a hodnotou "value" do hashmapy - pass + """Přidá prvek s klíčem `key` a hodnotou `value` do hashmapy.""" + index = self.hash_function(key) + start_index = index # Pro detekci plné tabulky + + while self.table[index] is not None: + # Přepiš hodnotu, pokud se shoduje klíč + existing_key, _ = self.table[index] + if existing_key == key: + self.table[index] = (key, value) + return + # Lineární probing + index = (index + 1) % self.size + if index == start_index: + raise Exception("Hash table is full!") + + # Uložení nového páru (key, value) + self.table[index] = (key, value) def find(self, key): - # TODO: Najít prvek s klíčem "key" v hashmapě a vrátit jeho hodnotu - pass + """Najde a vrátí hodnotu spojenou s klíčem `key`. Pokud klíč neexistuje, vrátí `None`.""" + index = self.hash_function(key) + start_index = index # Pro detekci cyklu + + while self.table[index] is not None: + existing_key, value = self.table[index] + if existing_key == key: + return value + # Lineární probing + index = (index + 1) % self.size + if index == start_index: + break + + return None def measure_time(operation, *args): + """Změří dobu trvání operace.""" start = time.time() result = operation(*args) end = time.time() return result, end - start if __name__ == "__main__": - hash_map = OpenAddressingHashMap(200000) + hash_map = OpenAddressingHashMap(200000) # Velikost tabulky musí být větší než počet klíčů data = list(range(1, 100001)) random.shuffle(data) diff --git a/04_built_in_hashmap.py b/04_built_in_hashmap.py index 2fd4f10..ffe262a 100644 --- a/04_built_in_hashmap.py +++ b/04_built_in_hashmap.py @@ -2,18 +2,23 @@ import random def measure_time(operation, *args): + """Změří dobu trvání operace.""" start = time.time() result = operation(*args) end = time.time() return result, end - start def add_elements(hash_map, keys, values): - # TODO: Přidat všechny klíče a hodnoty do hashmapy - pass + """Přidá všechny klíče a odpovídající hodnoty do hashmapy.""" + for key, value in zip(keys, values): + hash_map[key] = value def find_elements(hash_map, keys): - # TODO: Najít všechny klíče v hashmapě - pass + """Najde všechny klíče v hashmapě a vrátí odpovídající hodnoty.""" + results = [] + for key in keys: + results.append(hash_map.get(key)) + return results if __name__ == "__main__": hash_map = {}