diff --git a/01_pole.py b/01_pole.py index 0ef2919..8804b15 100644 --- a/01_pole.py +++ b/01_pole.py @@ -9,11 +9,14 @@ def measure_time(operation, *args): 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 + result = [] + for el in elements: + result.append(el in array) + return result if __name__ == "__main__": array = [] diff --git a/02_hashmap_chaining.py b/02_hashmap_chaining.py index fa3eba3..17a79d4 100644 --- a/02_hashmap_chaining.py +++ b/02_hashmap_chaining.py @@ -8,15 +8,24 @@ def __init__(self, size): def hash_function(self, key): # TODO: Implementovat hashovací funkci - pass + return hash(key) % self.size def add(self, key, value): # TODO: Přidat prvek s klíčem "key" a hodnotou "value" do hashmapy - pass + index = self.hash_function(key) + for i, (existing_key, _) in enumerate(self.table[index]): + if existing_key == key: + self.table[index][i] = (key, value) + return + 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 + index = self.hash_function(key) + for existing_key, value in self.table[index]: + if existing_key == key: + return value + return None def measure_time(operation, *args): start = time.time() diff --git a/03_hashmap_open_addressing.py b/03_hashmap_open_addressing.py index 95d0a4f..1686a6d 100644 --- a/03_hashmap_open_addressing.py +++ b/03_hashmap_open_addressing.py @@ -8,15 +8,28 @@ def __init__(self, size): def hash_function(self, key): # TODO: Implementovat hashovací funkci - pass + return hash(key) % self.size def add(self, key, value): # TODO: Přidat prvek s klíčem "key" a hodnotou "value" do hashmapy - pass + index = self.hash_function(key) + while self.table[index] is not None: + existing_key, _ = self.table[index] + if existing_key == key: + self.table[index] = (key, value) + return + index = (index + 1) % self.size + 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 + index = self.hash_function(key) + while self.table[index] is not True: + existing_key, value = self.table[index] + if existing_key == key: + return value + index = (index + 1) % self.size + return None def measure_time(operation, *args): start = time.time() diff --git a/04_built_in_hashmap.py b/04_built_in_hashmap.py index 2fd4f10..336b227 100644 --- a/04_built_in_hashmap.py +++ b/04_built_in_hashmap.py @@ -9,11 +9,12 @@ def measure_time(operation, *args): def add_elements(hash_map, keys, values): # TODO: Přidat všechny klíče a hodnoty do hashmapy - pass + 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 + return [hash_map.get(key, None) for key in keys] if __name__ == "__main__": hash_map = {}