mirror of
https://codeberg.org/andyscott/HashMaps.git
synced 2025-01-04 14:35:55 -05:00
Fix testing indentation
This commit is contained in:
parent
bd89c86984
commit
155571e227
1 changed files with 238 additions and 223 deletions
|
@ -1,11 +1,8 @@
|
||||||
# Name: Andrew Scott
|
# Author: Andrew Scott
|
||||||
# OSU Email: scottand@oregonstate.edu
|
# Description: HashMap implementation using Separate Chaining for collision
|
||||||
# Course: CS261 - Data Structures
|
# resolution
|
||||||
# Assignment: 6
|
|
||||||
# Due Date: 2022-06-03
|
|
||||||
# Description: HashMap implementation using Separate Chaining
|
|
||||||
|
|
||||||
from a6_include import (
|
from hm_include import (
|
||||||
DynamicArray,
|
DynamicArray,
|
||||||
LinkedList,
|
LinkedList,
|
||||||
SLNode,
|
SLNode,
|
||||||
|
@ -15,12 +12,19 @@ from a6_include import (
|
||||||
|
|
||||||
|
|
||||||
class HashMap:
|
class HashMap:
|
||||||
|
"""
|
||||||
|
Hash map that uses separate chaining for colision resolution
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
capacity : int
|
||||||
|
Initial capacity for the hash map
|
||||||
|
function : function
|
||||||
|
Hash function to use
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, capacity: int, function) -> None:
|
def __init__(self, capacity: int, function) -> None:
|
||||||
"""
|
"""Constructor for the HashMap class"""
|
||||||
Initialize new HashMap that uses
|
|
||||||
separate chaining for collision resolution
|
|
||||||
DO NOT CHANGE THIS METHOD IN ANY WAY
|
|
||||||
"""
|
|
||||||
self._buckets = DynamicArray()
|
self._buckets = DynamicArray()
|
||||||
for _ in range(capacity):
|
for _ in range(capacity):
|
||||||
self._buckets.append(LinkedList())
|
self._buckets.append(LinkedList())
|
||||||
|
@ -30,30 +34,40 @@ class HashMap:
|
||||||
self._size = 0
|
self._size = 0
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
|
"""Override string method to provide more readable output
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
str
|
||||||
|
Hash map in human readable form
|
||||||
"""
|
"""
|
||||||
Override string method to provide more readable output
|
|
||||||
DO NOT CHANGE THIS METHOD IN ANY WAY
|
|
||||||
"""
|
|
||||||
out = ""
|
out = ""
|
||||||
for i in range(self._buckets.length()):
|
for i in range(self._buckets.length()):
|
||||||
out += str(i) + ": " + str(self._buckets[i]) + "\n"
|
out += str(i) + ": " + str(self._buckets[i]) + "\n"
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def get_size(self) -> int:
|
def get_size(self) -> int:
|
||||||
|
"""Get the map size
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
int
|
||||||
|
Size of the hash map
|
||||||
"""
|
"""
|
||||||
Return size of map
|
|
||||||
DO NOT CHANGE THIS METHOD IN ANY WAY
|
|
||||||
"""
|
|
||||||
return self._size
|
return self._size
|
||||||
|
|
||||||
def get_capacity(self) -> int:
|
def get_capacity(self) -> int:
|
||||||
"""
|
"""Get the map capacity
|
||||||
Return capacity of map
|
|
||||||
DO NOT CHANGE THIS METHOD IN ANY WAY
|
|
||||||
"""
|
|
||||||
return self._capacity
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------ #
|
Returns
|
||||||
|
-------
|
||||||
|
int
|
||||||
|
Capacity of the hash map
|
||||||
|
"""
|
||||||
|
|
||||||
|
return self._capacity
|
||||||
|
|
||||||
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
|
"""Adds (or updates) a key/value pair to the hash map
|
||||||
|
@ -280,6 +294,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
|
|
||||||
# ------------------- BASIC TESTING ---------------------------------------- #
|
# ------------------- BASIC TESTING ---------------------------------------- #
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
print("\nPDF - put example 1")
|
print("\nPDF - put example 1")
|
||||||
|
@ -311,7 +326,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
m.put("key4", 40)
|
m.put("key4", 40)
|
||||||
print(m.empty_buckets(), m.get_size(), m.get_capacity())
|
print(m.empty_buckets(), m.get_size(), m.get_capacity())
|
||||||
|
|
||||||
print("\nPDF - empty_buckets example 2")
|
print("\n empty_buckets example 2")
|
||||||
print("-----------------------------")
|
print("-----------------------------")
|
||||||
m = HashMap(50, hash_function_1)
|
m = HashMap(50, hash_function_1)
|
||||||
for i in range(150):
|
for i in range(150):
|
||||||
|
@ -319,7 +334,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
if i % 30 == 0:
|
if i % 30 == 0:
|
||||||
print(m.empty_buckets(), m.get_size(), m.get_capacity())
|
print(m.empty_buckets(), m.get_size(), m.get_capacity())
|
||||||
|
|
||||||
print("\nPDF - table_load example 1")
|
print("\n table_load example 1")
|
||||||
print("--------------------------")
|
print("--------------------------")
|
||||||
m = HashMap(100, hash_function_1)
|
m = HashMap(100, hash_function_1)
|
||||||
print(m.table_load())
|
print(m.table_load())
|
||||||
|
@ -330,7 +345,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
m.put("key1", 30)
|
m.put("key1", 30)
|
||||||
print(m.table_load())
|
print(m.table_load())
|
||||||
|
|
||||||
print("\nPDF - table_load example 2")
|
print("\n table_load example 2")
|
||||||
print("--------------------------")
|
print("--------------------------")
|
||||||
m = HashMap(50, hash_function_1)
|
m = HashMap(50, hash_function_1)
|
||||||
for i in range(50):
|
for i in range(50):
|
||||||
|
@ -338,7 +353,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
if i % 10 == 0:
|
if i % 10 == 0:
|
||||||
print(m.table_load(), m.get_size(), m.get_capacity())
|
print(m.table_load(), m.get_size(), m.get_capacity())
|
||||||
|
|
||||||
print("\nPDF - clear example 1")
|
print("\n clear example 1")
|
||||||
print("---------------------")
|
print("---------------------")
|
||||||
m = HashMap(100, hash_function_1)
|
m = HashMap(100, hash_function_1)
|
||||||
print(m.get_size(), m.get_capacity())
|
print(m.get_size(), m.get_capacity())
|
||||||
|
@ -349,7 +364,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
m.clear()
|
m.clear()
|
||||||
print(m.get_size(), m.get_capacity())
|
print(m.get_size(), m.get_capacity())
|
||||||
|
|
||||||
print("\nPDF - clear example 2")
|
print("\n clear example 2")
|
||||||
print("---------------------")
|
print("---------------------")
|
||||||
m = HashMap(50, hash_function_1)
|
m = HashMap(50, hash_function_1)
|
||||||
print(m.get_size(), m.get_capacity())
|
print(m.get_size(), m.get_capacity())
|
||||||
|
@ -362,7 +377,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
m.clear()
|
m.clear()
|
||||||
print(m.get_size(), m.get_capacity())
|
print(m.get_size(), m.get_capacity())
|
||||||
|
|
||||||
print("\nPDF - resize example 1")
|
print("\n resize example 1")
|
||||||
print("----------------------")
|
print("----------------------")
|
||||||
m = HashMap(20, hash_function_1)
|
m = HashMap(20, hash_function_1)
|
||||||
m.put("key1", 10)
|
m.put("key1", 10)
|
||||||
|
@ -370,7 +385,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
m.resize_table(30)
|
m.resize_table(30)
|
||||||
print(m.get_size(), m.get_capacity(), m.get("key1"), m.contains_key("key1"))
|
print(m.get_size(), m.get_capacity(), m.get("key1"), m.contains_key("key1"))
|
||||||
|
|
||||||
print("\nPDF - resize example 2")
|
print("\n resize example 2")
|
||||||
print("----------------------")
|
print("----------------------")
|
||||||
m = HashMap(75, hash_function_2)
|
m = HashMap(75, hash_function_2)
|
||||||
keys = [i for i in range(1, 1000, 13)]
|
keys = [i for i in range(1, 1000, 13)]
|
||||||
|
@ -398,14 +413,14 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
round(m.table_load(), 2),
|
round(m.table_load(), 2),
|
||||||
)
|
)
|
||||||
|
|
||||||
print("\nPDF - get example 1")
|
print("\n get example 1")
|
||||||
print("-------------------")
|
print("-------------------")
|
||||||
m = HashMap(30, hash_function_1)
|
m = HashMap(30, hash_function_1)
|
||||||
print(m.get("key"))
|
print(m.get("key"))
|
||||||
m.put("key1", 10)
|
m.put("key1", 10)
|
||||||
print(m.get("key1"))
|
print(m.get("key1"))
|
||||||
|
|
||||||
print("\nPDF - get example 2")
|
print("\n get example 2")
|
||||||
print("-------------------")
|
print("-------------------")
|
||||||
m = HashMap(150, hash_function_2)
|
m = HashMap(150, hash_function_2)
|
||||||
for i in range(200, 300, 7):
|
for i in range(200, 300, 7):
|
||||||
|
@ -415,7 +430,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
print(i, m.get(str(i)), m.get(str(i)) == i * 10)
|
print(i, m.get(str(i)), m.get(str(i)) == i * 10)
|
||||||
print(i + 1, m.get(str(i + 1)), m.get(str(i + 1)) == (i + 1) * 10)
|
print(i + 1, m.get(str(i + 1)), m.get(str(i + 1)) == (i + 1) * 10)
|
||||||
|
|
||||||
print("\nPDF - contains_key example 1")
|
print("\n contains_key example 1")
|
||||||
print("----------------------------")
|
print("----------------------------")
|
||||||
m = HashMap(10, hash_function_1)
|
m = HashMap(10, hash_function_1)
|
||||||
print(m.contains_key("key1"))
|
print(m.contains_key("key1"))
|
||||||
|
@ -429,7 +444,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
m.remove("key3")
|
m.remove("key3")
|
||||||
print(m.contains_key("key3"))
|
print(m.contains_key("key3"))
|
||||||
|
|
||||||
print("\nPDF - contains_key example 2")
|
print("\n contains_key example 2")
|
||||||
print("----------------------------")
|
print("----------------------------")
|
||||||
m = HashMap(75, hash_function_2)
|
m = HashMap(75, hash_function_2)
|
||||||
keys = [i for i in range(1, 1000, 20)]
|
keys = [i for i in range(1, 1000, 20)]
|
||||||
|
@ -444,7 +459,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
result &= not m.contains_key(str(key + 1))
|
result &= not m.contains_key(str(key + 1))
|
||||||
print(result)
|
print(result)
|
||||||
|
|
||||||
print("\nPDF - remove example 1")
|
print("\n remove example 1")
|
||||||
print("----------------------")
|
print("----------------------")
|
||||||
m = HashMap(50, hash_function_1)
|
m = HashMap(50, hash_function_1)
|
||||||
print(m.get("key1"))
|
print(m.get("key1"))
|
||||||
|
@ -454,7 +469,7 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
print(m.get("key1"))
|
print(m.get("key1"))
|
||||||
m.remove("key4")
|
m.remove("key4")
|
||||||
|
|
||||||
print("\nPDF - get_keys example 1")
|
print("\n get_keys example 1")
|
||||||
print("------------------------")
|
print("------------------------")
|
||||||
m = HashMap(10, hash_function_2)
|
m = HashMap(10, hash_function_2)
|
||||||
for i in range(100, 200, 10):
|
for i in range(100, 200, 10):
|
||||||
|
@ -469,14 +484,14 @@ def find_mode(da: DynamicArray) -> (DynamicArray, int):
|
||||||
m.resize_table(2)
|
m.resize_table(2)
|
||||||
print(m.get_keys())
|
print(m.get_keys())
|
||||||
|
|
||||||
print("\nPDF - find_mode example 1")
|
print("\n find_mode example 1")
|
||||||
print("-----------------------------")
|
print("-----------------------------")
|
||||||
da = DynamicArray(["apple", "apple", "grape", "melon", "melon", "peach"])
|
da = DynamicArray(["apple", "apple", "grape", "melon", "melon", "peach"])
|
||||||
map = HashMap(da.length() // 3, hash_function_1)
|
map = HashMap(da.length() // 3, hash_function_1)
|
||||||
mode, frequency = find_mode(da)
|
mode, frequency = find_mode(da)
|
||||||
print(f"Input: {da}\nMode: {mode}, Frequency: {frequency}")
|
print(f"Input: {da}\nMode: {mode}, Frequency: {frequency}")
|
||||||
|
|
||||||
print("\nPDF - find_mode example 2")
|
print("\n find_mode example 2")
|
||||||
print("-----------------------------")
|
print("-----------------------------")
|
||||||
test_cases = (
|
test_cases = (
|
||||||
[
|
[
|
||||||
|
|
Loading…
Reference in a new issue