From 3b02a2e1235fe6f6365117bc7232980a7c0dabb6 Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Fri, 27 Oct 2023 11:29:00 +0530 Subject: [PATCH 1/7] Added doctest to hash_table.py --- data_structures/hashing/hash_table.py | 102 ++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/data_structures/hashing/hash_table.py b/data_structures/hashing/hash_table.py index 5bf431328da4..02192107d71a 100644 --- a/data_structures/hashing/hash_table.py +++ b/data_structures/hashing/hash_table.py @@ -52,6 +52,28 @@ def balanced_factor(self): ) def hash_function(self, key): + """ + Generates hash for the given key value + + Examples: + + Creating HashTable with size 5 + >>> ht = HashTable(5) + >>> ht.hash_function(10) + 0 + >>> ht.hash_function(20) + 0 + >>> ht.hash_function(4) + 4 + >>> ht.hash_function(18) + 3 + >>> ht.hash_function(-18) + 2 + >>> ht.hash_function(0) + 0 + >>> ht.hash_function(-0) + 0 + """ return key % self.size_table def _step_by_step(self, step_ord): @@ -105,10 +127,90 @@ def bulk_insert(self, values): i += 1 def _set_value(self, key, data): + """ + _set_value functions allows to update value at a particular hash + + Examples: + 1. _set_value in HashTable of size 5 + >>> ht = HashTable(5) + >>> ht.insert_data(10) + >>> ht.insert_data(20) + >>> ht.insert_data(30) + >>> ht._set_value(0,15) + >>> ht.keys() + {0: 15, 1: 20, 2: 30} + + 2. _set_value in HashTable of size 2 + >>> ht = HashTable(2) + >>> ht.insert_data(17) + >>> ht.insert_data(18) + >>> ht.insert_data(99) + >>> ht._set_value(3,15) + >>> ht.keys() + {3: 15, 2: 17, 4: 99} + + 3. _set_value in HashTable when hash is not present + >>> ht = HashTable(2) + >>> ht.insert_data(17) + >>> ht.insert_data(18) + >>> ht.insert_data(99) + >>> ht._set_value(0,15) + >>> ht.keys() + {3: 18, 2: 17, 4: 99, 0: 15} + + 4. _set_value in HashTable when multiple hash are not present + >>> ht = HashTable(2) + >>> ht.insert_data(17) + >>> ht.insert_data(18) + >>> ht.insert_data(99) + >>> ht._set_value(0,15) + >>> ht._set_value(1,20) + >>> ht.keys() + {3: 18, 2: 17, 4: 99, 0: 15, 1: 20} + """ self.values[key] = data self._keys[key] = data def _collision_resolution(self, key, data=None): + """ + This method is a type of open addressing which is used for handling collision. + + In this implementation the concept of linear probing has been used. + + The hash table is searched sequentially from the original location of the + hash, if the new hash/location we get is already occupied we check for the next + hash/location. + + references: + - https://en.wikipedia.org/wiki/Linear_probing + + Examples: + 1. The collission will be with keys 18 & 99, so new hash will be created for 99 + >>> ht = HashTable(3) + >>> ht.insert_data(17) + >>> ht.insert_data(18) + >>> ht.insert_data(99) + >>> ht.keys() + {2: 17, 0: 18, 1: 99} + + 2. The collission will be with keys 17 & 101, so new hash + will be created for 101 + >>> ht = HashTable(4) + >>> ht.insert_data(17) + >>> ht.insert_data(18) + >>> ht.insert_data(99) + >>> ht.insert_data(101) + >>> ht.keys() + {1: 17, 2: 18, 3: 99, 0: 101} + + 2. The collission will be with all keys, so new hash will be created for all + >>> ht = HashTable(1) + >>> ht.insert_data(17) + >>> ht.insert_data(18) + >>> ht.insert_data(99) + >>> ht.keys() + {2: 17, 3: 18, 4: 99} + """ new_key = self.hash_function(key + 1) while self.values[new_key] is not None and self.values[new_key] != key: From 35d171efa55bc1ff5842b1ebb218ced0c72df47f Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Fri, 27 Oct 2023 12:22:21 +0530 Subject: [PATCH 2/7] Update hash_table.py --- data_structures/hashing/hash_table.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/hashing/hash_table.py b/data_structures/hashing/hash_table.py index 02192107d71a..44056f6a155a 100644 --- a/data_structures/hashing/hash_table.py +++ b/data_structures/hashing/hash_table.py @@ -185,7 +185,7 @@ def _collision_resolution(self, key, data=None): - https://en.wikipedia.org/wiki/Linear_probing Examples: - 1. The collission will be with keys 18 & 99, so new hash will be created for 99 + 1. The collision will be with keys 18 & 99, so new hash will be created for 99 >>> ht = HashTable(3) >>> ht.insert_data(17) >>> ht.insert_data(18) @@ -193,7 +193,7 @@ def _collision_resolution(self, key, data=None): >>> ht.keys() {2: 17, 0: 18, 1: 99} - 2. The collission will be with keys 17 & 101, so new hash + 2. The collision will be with keys 17 & 101, so new hash will be created for 101 >>> ht = HashTable(4) >>> ht.insert_data(17) @@ -203,7 +203,7 @@ def _collision_resolution(self, key, data=None): >>> ht.keys() {1: 17, 2: 18, 3: 99, 0: 101} - 2. The collission will be with all keys, so new hash will be created for all + 2. The collision will be with all keys, so new hash will be created for all >>> ht = HashTable(1) >>> ht.insert_data(17) >>> ht.insert_data(18) From 147f7faeea1a2b51a14e99f653d4a242210a9530 Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Fri, 27 Oct 2023 16:43:37 +0530 Subject: [PATCH 3/7] Update hash_table.py --- data_structures/hashing/hash_table.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/data_structures/hashing/hash_table.py b/data_structures/hashing/hash_table.py index 44056f6a155a..5b05d29f6f4d 100644 --- a/data_structures/hashing/hash_table.py +++ b/data_structures/hashing/hash_table.py @@ -69,6 +69,8 @@ def hash_function(self, key): 3 >>> ht.hash_function(-18) 2 + >>> ht.hash_function(18.5) + 3.5 >>> ht.hash_function(0) 0 >>> ht.hash_function(-0) @@ -185,7 +187,7 @@ def _collision_resolution(self, key, data=None): - https://en.wikipedia.org/wiki/Linear_probing Examples: - 1. The collision will be with keys 18 & 99, so new hash will be created for 99 + 1. The collission will be with keys 18 & 99, so new hash will be created for 99 >>> ht = HashTable(3) >>> ht.insert_data(17) >>> ht.insert_data(18) @@ -193,7 +195,7 @@ def _collision_resolution(self, key, data=None): >>> ht.keys() {2: 17, 0: 18, 1: 99} - 2. The collision will be with keys 17 & 101, so new hash + 2. The collission will be with keys 17 & 101, so new hash will be created for 101 >>> ht = HashTable(4) >>> ht.insert_data(17) @@ -203,13 +205,22 @@ def _collision_resolution(self, key, data=None): >>> ht.keys() {1: 17, 2: 18, 3: 99, 0: 101} - 2. The collision will be with all keys, so new hash will be created for all + 2. The collission will be with all keys, so new hash will be created for all >>> ht = HashTable(1) >>> ht.insert_data(17) >>> ht.insert_data(18) >>> ht.insert_data(99) >>> ht.keys() {2: 17, 3: 18, 4: 99} + + 2. The collission will be with all keys, so new hash will be created for all + >>> ht = HashTable(1) + >>> ht.insert_data(17) + >>> ht.insert_data(18) + >>> ht.insert_data(99.99) + Traceback (most recent call last): + ... + TypeError: list indices must be integers or slices, not float """ new_key = self.hash_function(key + 1) From 6c1da678e47d17a2e420cb4bae2b77279bbdc82b Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Fri, 27 Oct 2023 16:45:59 +0530 Subject: [PATCH 4/7] Update hash_table.py --- data_structures/hashing/hash_table.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/hashing/hash_table.py b/data_structures/hashing/hash_table.py index 5b05d29f6f4d..43aa9d837bc0 100644 --- a/data_structures/hashing/hash_table.py +++ b/data_structures/hashing/hash_table.py @@ -213,7 +213,7 @@ def _collision_resolution(self, key, data=None): >>> ht.keys() {2: 17, 3: 18, 4: 99} - 2. The collission will be with all keys, so new hash will be created for all + 3. Trying to insert float key in hash >>> ht = HashTable(1) >>> ht.insert_data(17) >>> ht.insert_data(18) From 2e47f71f729182f6656877cfae0ab62785c6e90b Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:00:26 +0530 Subject: [PATCH 5/7] Update hash_table.py --- data_structures/hashing/hash_table.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/hashing/hash_table.py b/data_structures/hashing/hash_table.py index 43aa9d837bc0..7fe57068f6a3 100644 --- a/data_structures/hashing/hash_table.py +++ b/data_structures/hashing/hash_table.py @@ -187,7 +187,7 @@ def _collision_resolution(self, key, data=None): - https://en.wikipedia.org/wiki/Linear_probing Examples: - 1. The collission will be with keys 18 & 99, so new hash will be created for 99 + 1. The collision will be with keys 18 & 99, so new hash will be created for 99 >>> ht = HashTable(3) >>> ht.insert_data(17) >>> ht.insert_data(18) @@ -195,7 +195,7 @@ def _collision_resolution(self, key, data=None): >>> ht.keys() {2: 17, 0: 18, 1: 99} - 2. The collission will be with keys 17 & 101, so new hash + 2. The collision will be with keys 17 & 101, so new hash will be created for 101 >>> ht = HashTable(4) >>> ht.insert_data(17) @@ -205,7 +205,7 @@ def _collision_resolution(self, key, data=None): >>> ht.keys() {1: 17, 2: 18, 3: 99, 0: 101} - 2. The collission will be with all keys, so new hash will be created for all + 2. The collision will be with all keys, so new hash will be created for all >>> ht = HashTable(1) >>> ht.insert_data(17) >>> ht.insert_data(18) From 384a281a9e2725ba829c0ab116464404dc0eb217 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 27 Oct 2023 14:07:32 +0200 Subject: [PATCH 6/7] Apply suggestions from code review --- data_structures/hashing/hash_table.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/data_structures/hashing/hash_table.py b/data_structures/hashing/hash_table.py index 7fe57068f6a3..0e62f4262e2f 100644 --- a/data_structures/hashing/hash_table.py +++ b/data_structures/hashing/hash_table.py @@ -70,6 +70,8 @@ def hash_function(self, key): >>> ht.hash_function(-18) 2 >>> ht.hash_function(18.5) + 2 + >>> ht.hash_function(18.5) 3.5 >>> ht.hash_function(0) 0 @@ -209,7 +211,7 @@ def _collision_resolution(self, key, data=None): >>> ht = HashTable(1) >>> ht.insert_data(17) >>> ht.insert_data(18) - >>> ht.insert_data(99) + >>> ht.insert_data(99.99) >>> ht.keys() {2: 17, 3: 18, 4: 99} From bd4bba4c3e2b81b1f72efc80e25b766e02c4fa9b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 27 Oct 2023 14:09:20 +0200 Subject: [PATCH 7/7] Update hash_table.py --- data_structures/hashing/hash_table.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/data_structures/hashing/hash_table.py b/data_structures/hashing/hash_table.py index 0e62f4262e2f..7fe57068f6a3 100644 --- a/data_structures/hashing/hash_table.py +++ b/data_structures/hashing/hash_table.py @@ -70,8 +70,6 @@ def hash_function(self, key): >>> ht.hash_function(-18) 2 >>> ht.hash_function(18.5) - 2 - >>> ht.hash_function(18.5) 3.5 >>> ht.hash_function(0) 0 @@ -211,7 +209,7 @@ def _collision_resolution(self, key, data=None): >>> ht = HashTable(1) >>> ht.insert_data(17) >>> ht.insert_data(18) - >>> ht.insert_data(99.99) + >>> ht.insert_data(99) >>> ht.keys() {2: 17, 3: 18, 4: 99}