From d0caad7862a6a57361da756bc238d38c31f653e5 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Thu, 2 Jun 2022 15:29:46 -0400 Subject: [PATCH] Implemented remove and get_keys methods --- hash_map_sc.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/hash_map_sc.py b/hash_map_sc.py index 3e588f2..ba89a66 100644 --- a/hash_map_sc.py +++ b/hash_map_sc.py @@ -185,16 +185,35 @@ class HashMap: def remove(self, key: str) -> None: + """Removes a key/value pair from the hash map + + Parameters + ---------- + key : str + Key to look up in the hash map """ - TODO: Write this implementation - """ - pass + + hash = self._hash_function(key) + index = hash % self._capacity + is_removed = self._buckets[index].remove(key) + if is_removed: + self._size -= 1 def get_keys(self) -> DynamicArray: + """Get an array that contains all the keys in the hash map + + Returns + ------- + DynamicArray + Array containing the hash maps keys """ - TODO: Write this implementation - """ - pass + + keys = DynamicArray() + for i in range(self._capacity): + linked_list = self._buckets[i] + for node in linked_list: + keys.append(node.key) + return keys def find_mode(da: DynamicArray) -> (DynamicArray, int):