From 8a6f0b8d20ea0b02680ed0342a06e154cbc88ea3 Mon Sep 17 00:00:00 2001 From: seanacres <6416690+seanacres@users.noreply.github.com> Date: Wed, 28 Oct 2020 11:53:36 -0400 Subject: [PATCH] Implemented day 1 with DJB2 --- hashtable/hashtable.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 0205f0ba9..09cbfad47 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -21,7 +21,8 @@ class HashTable: """ def __init__(self, capacity): - # Your code here + self.capacity = MIN_CAPACITY + self.array = [None] * capacity def get_num_slots(self): @@ -35,6 +36,7 @@ def get_num_slots(self): Implement this. """ # Your code here + return self.capacity def get_load_factor(self): @@ -62,7 +64,10 @@ def djb2(self, key): Implement this, and/or FNV-1. """ - # Your code here + hash = 5381 + for c in key: + hash = (hash * 33) + ord(c) + return hash def hash_index(self, key): @@ -82,6 +87,8 @@ def put(self, key, value): Implement this. """ # Your code here + i = self.hash_index(key) + self.array[i] = value def delete(self, key): @@ -93,6 +100,8 @@ def delete(self, key): Implement this. """ # Your code here + i = self.hash_index(key) + self.array[i] = None def get(self, key): @@ -104,6 +113,8 @@ def get(self, key): Implement this. """ # Your code here + i = self.hash_index(key) + return self.array[i] def resize(self, new_capacity):