Implemented the put, empty_buckets, table_load, and clear methods

This commit is contained in:
Andrew Scott 2022-06-02 13:49:09 -04:00
parent 82a1c116dc
commit b2ffb5a579
Signed by: a
GPG key ID: 3EB62D0BBB8DB381

View file

@ -6,7 +6,7 @@
# Description: HashMap implementation using Separate Chaining # Description: HashMap implementation using Separate Chaining
from a6_include import DynamicArray, LinkedList, hash_function_1, hash_function_2 from a6_include import DynamicArray, LinkedList, SLNode, hash_function_1, hash_function_2
class HashMap: class HashMap:
@ -51,28 +51,58 @@ class HashMap:
# ------------------------------------------------------------------ # # ------------------------------------------------------------------ #
def put(self, key: str, value: object) -> None: def put(self, key: str, value: object) -> None:
"""Adds (or updates) a key/value pair to the hash map
Parameters
----------
key : str
Identifier for the given value
value : object
Value to add, or update if the key is already present
""" """
TODO: Write this implementation
""" hash = self._hash_function(key)
pass index = hash % self._capacity
node = self._buckets[index].contains(key)
if node is None:
self._buckets[index].insert(key, value)
self._size += 1
else:
node.value = value
def empty_buckets(self) -> int: def empty_buckets(self) -> int:
"""Gets the number of empty buckets in the hash table
Returns
-------
int
Number of empty buckets
""" """
TODO: Write this implementation
""" count = 0
pass for index in range(self._capacity):
if self._buckets[index].length() == 0:
count += 1
return count
def table_load(self) -> float: def table_load(self) -> float:
"""Get the current hash table load factor
Returns
-------
float
The load factor
""" """
TODO: Write this implementation
""" return self._size / self._capacity
pass
def clear(self) -> None: def clear(self) -> None:
""" """Clear the contents of the hash map without changing its capacity"""
TODO: Write this implementation for index in range(self._capacity):
""" if self._buckets[index].length() != 0:
pass self._buckets[index] = LinkedList()
self._size = 0
def resize_table(self, new_capacity: int) -> None: def resize_table(self, new_capacity: int) -> None:
""" """