From 34f1e23e7636fed249d9a7557e46d6350c191927 Mon Sep 17 00:00:00 2001 From: "3.E Weich Jakub" Date: Mon, 18 Nov 2024 12:46:41 +0100 Subject: [PATCH 1/4] 03 Done --- 01_pole.py | 6 +++++- 03_hashmap_open_addressing.py | 21 +++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/01_pole.py b/01_pole.py index 0ef2919..13803af 100644 --- a/01_pole.py +++ b/01_pole.py @@ -9,11 +9,15 @@ def measure_time(operation, *args): def add_elements(array, elements): # TODO: Přidat všechny prvky z "elements" do pole "array" + array.extend(elements); pass def find_elements(array, elements): # TODO: Najít všechny prvky z "elements" v poli "array" - pass + results = [] + for el in elements: + results.append(el in elements) + return results if __name__ == "__main__": array = [] diff --git a/03_hashmap_open_addressing.py b/03_hashmap_open_addressing.py index 95d0a4f..a26edd3 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 None: + if self.table[index][0] == key: + return self.table[index][1] + index = (index +1) % self.size + return None def measure_time(operation, *args): start = time.time() From 0d52115fea86b0e85efc31459581c8c5e300b8f4 Mon Sep 17 00:00:00 2001 From: "3.E Weich Jakub" Date: Mon, 18 Nov 2024 12:59:12 +0100 Subject: [PATCH 2/4] 02 Done --- 02_hashmap_chaining.py | 15 ++++++++++++--- 03_hashmap_open_addressing.py | 1 - 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/02_hashmap_chaining.py b/02_hashmap_chaining.py index fa3eba3..a210989 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, value) in enumerate(self.table[index]): + if existing_key == key: + self.table[index][i] = (key, value) + return 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 def measure_time(operation, *args): start = time.time() diff --git a/03_hashmap_open_addressing.py b/03_hashmap_open_addressing.py index a26edd3..559d514 100644 --- a/03_hashmap_open_addressing.py +++ b/03_hashmap_open_addressing.py @@ -29,7 +29,6 @@ def find(self, key): if self.table[index][0] == key: return self.table[index][1] index = (index +1) % self.size - return None def measure_time(operation, *args): start = time.time() From 46282d2a97a32de2088dd1b996b90b7992f06c94 Mon Sep 17 00:00:00 2001 From: "3.E Weich Jakub" Date: Mon, 18 Nov 2024 13:09:54 +0100 Subject: [PATCH 3/4] 04 Done --- 04_built_in_hashmap.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/04_built_in_hashmap.py b/04_built_in_hashmap.py index 2fd4f10..e5d3780 100644 --- a/04_built_in_hashmap.py +++ b/04_built_in_hashmap.py @@ -9,12 +9,16 @@ 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 - + results = [] + for key in keys: + results.append(hash_map.get(key)) + return results + if __name__ == "__main__": hash_map = {} data = list(range(1, 100001)) From 98c0201113d783fc2b124488745bc01a26688a9d Mon Sep 17 00:00:00 2001 From: "3.E Weich Jakub" Date: Mon, 18 Nov 2024 13:15:24 +0100 Subject: [PATCH 4/4] =?UTF-8?q?=C3=BAprava=2003?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03_hashmap_open_addressing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/03_hashmap_open_addressing.py b/03_hashmap_open_addressing.py index 559d514..89469ac 100644 --- a/03_hashmap_open_addressing.py +++ b/03_hashmap_open_addressing.py @@ -15,7 +15,7 @@ def add(self, key, value): index = self.hash_function(key) while self.table[index] is not None: - existing_key, _ = self.table[index] + existing_key, value = self.table[index] if existing_key == key: self.table[index] = (key, value) return