From 3f9e87cf1e273916cd9c7d62ed1f93a429423cc1 Mon Sep 17 00:00:00 2001 From: delfin mongendu Date: Sat, 31 Oct 2020 08:28:51 -0400 Subject: [PATCH 1/5] init --- hashtable/hashtable.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 0205f0ba9..83e19f61f 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -22,6 +22,13 @@ class HashTable: def __init__(self, capacity): # Your code here + # Your code here + # initialize the hash table with empty storage list entries + # size of the array = min 8 + # will store our data in - set equal to None force python to get a list that has a fixed length + self.capacity = MIN_CAPACITY + self.bucket = [None] * self.capacity + self.count = 0 def get_num_slots(self): From b1081ad3db4640718e43a10c21ffbf8332bbffc7 Mon Sep 17 00:00:00 2001 From: delfin mongendu Date: Sat, 31 Oct 2020 08:34:35 -0400 Subject: [PATCH 2/5] repr --- hashtable/hashtable.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 83e19f61f..fd47392ae 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -30,6 +30,8 @@ def __init__(self, capacity): self.bucket = [None] * self.capacity self.count = 0 + def __repr__(self): + return str(self.capacity) def get_num_slots(self): """ From aebb87742e0895ad8ccdca47fcbafd7f33fe996a Mon Sep 17 00:00:00 2001 From: delfin mongendu Date: Sun, 1 Nov 2020 23:08:28 -0500 Subject: [PATCH 3/5] hash --- hashtable/hashtable.py | 43 ++++++++++-------------------------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index fd47392ae..d366b55f0 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -34,52 +34,29 @@ def __repr__(self): return str(self.capacity) def get_num_slots(self): - """ - Return the length of the list you're using to hold the hash - table data. (Not the number of items stored in the hash table, - but the number of slots in the main list.) - - One of the tests relies on this. - - Implement this. - """ # Your code here + return len(self.bucket) def get_load_factor(self): - """ - Return the load factor for this hash table. - - Implement this. - """ # Your code here + return self.count / len(self.bucket) def fnv1(self, key): - """ - FNV-1 Hash, 64-bit - - Implement this, and/or DJB2. - """ - - # Your code here - + pass + def djb2(self, key): - """ - DJB2 hash, 32-bit - - Implement this, and/or FNV-1. - """ - # Your code here + hash = 5381 + # iterates characters in key, + for character in key: + # ord: numerical value of that character --> + hash = ((hash << 5) + hash) + ord(character) + return hash & 0xFFFFFFFF def hash_index(self, key): - """ - Take an arbitrary key and return a valid integer index - between within the storage capacity of the hash table. - """ - #return self.fnv1(key) % self.capacity return self.djb2(key) % self.capacity def put(self, key, value): From c36f6050c0a261f98e9f6321a26c31902b5a48d0 Mon Sep 17 00:00:00 2001 From: delfin mongendu Date: Sun, 1 Nov 2020 23:27:02 -0500 Subject: [PATCH 4/5] hash1 --- hashtable/hashtable.py | 59 +++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index d366b55f0..93b33407e 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -60,36 +60,41 @@ def hash_index(self, key): return self.djb2(key) % self.capacity def put(self, key, value): - """ - Store the value with the given key. - - Hash collisions should be handled with Linked List Chaining. - - Implement this. - """ - # Your code here - + idx = self.hash_index(key) + + node = HashTableEntry(key,value) + + key = self.bucket[idx] + + self.count += 1 + + # the key exist + if key: + # overwrite with the node + self.bucket[idx] = node + self.bucket[idx].next = key + # if self.capacity[index] exist, + # use LL to set next to the repeated key and value + else: + self.bucket[idx] = node + # print('adding node', node.next) + print(self.bucket) + return self.bucket[idx] def delete(self, key): - """ - Remove the value stored with the given key. - - Print a warning if the key is not found. - - Implement this. - """ - # Your code here - - + self.count -= 1 + self.put(key, None) + def get(self, key): - """ - Retrieve the value stored with the given key. - - Returns None if the key is not found. - - Implement this. - """ - # Your code here + idx = self.hash_index(key) + + arr = self.bucket[idx] + + while arr: + if arr.key == key: + return arr.value + arr = arr.next + def resize(self, new_capacity): From e053119e92193f7eb70c3151ed8c79f75fa6ea75 Mon Sep 17 00:00:00 2001 From: delfin mongendu Date: Mon, 2 Nov 2020 23:56:17 -0500 Subject: [PATCH 5/5] getsize --- hashtable/hashtable.py | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 93b33407e..3485686ec 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -98,13 +98,38 @@ def get(self, key): def resize(self, new_capacity): - """ - Changes the capacity of the hash table and - rehashes all key/value pairs. - - Implement this. - """ - # Your code here + + if self.get_load_factor() < 0.2: + self.capacity = new_capacity // 2 + prev_bucket = self.bucket # [None, None, None, None, None, None, None, None]. + self.bucket = [None] * self.capacity # [None, None, None, None] + + for node in prev_bucket: # Traverse thru all the nodes. + + if node != None: + self.put( node.key, node.value ) + else: + continue + + + if self.get_load_factor() > 0.7: + self.capacity = new_capacity * 2 + prev_bucket = self.bucket # [None, None, None, None, None, None, None, None]. + self.bucket = [None] * self.capacity + + for node in prev_bucket: # Traverse thru all the nodes. + + if node != None: + self.put( node.key, node.value ) + else: + continue + + + + + + +