From 2d7511dc7baac35994b3815542874254573386e3 Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Fri, 27 Oct 2023 10:25:40 +0530 Subject: [PATCH 1/2] Added doctest to double_hash.py --- data_structures/hashing/double_hash.py | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/data_structures/hashing/double_hash.py b/data_structures/hashing/double_hash.py index be21e74cadd0..22d585eaab80 100644 --- a/data_structures/hashing/double_hash.py +++ b/data_structures/hashing/double_hash.py @@ -35,6 +35,33 @@ def __hash_double_function(self, key, data, increment): return (increment * self.__hash_function_2(key, data)) % self.size_table def _collision_resolution(self, key, data=None): + """ + Examples: + + 1. Try to add three data elements when the size is three + >>> dh = DoubleHash(3) + >>> dh.insert_data(10) + >>> dh.insert_data(20) + >>> dh.insert_data(30) + >>> dh.keys() + {1: 10, 2: 20, 0: 30} + + 2. Try to add three data elements when the size is two + >>> dh = DoubleHash(2) + >>> dh.insert_data(10) + >>> dh.insert_data(20) + >>> dh.insert_data(30) + >>> dh.keys() + {10: 10, 9: 20, 8: 30} + + 3. Try to add three data elements when the size is one + >>> dh = DoubleHash(2) + >>> dh.insert_data(10) + >>> dh.insert_data(20) + >>> dh.insert_data(30) + >>> dh.keys() + {10: 10, 9: 20, 8: 30} + """ i = 1 new_key = self.hash_function(data) @@ -50,3 +77,9 @@ def _collision_resolution(self, key, data=None): i += 1 return new_key + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 06e321b9e52eba6380c7e3ecd10e031763de20aa Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Fri, 27 Oct 2023 20:35:10 +0530 Subject: [PATCH 2/2] Update double_hash.py --- data_structures/hashing/double_hash.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/hashing/double_hash.py b/data_structures/hashing/double_hash.py index 22d585eaab80..76c6c86814ec 100644 --- a/data_structures/hashing/double_hash.py +++ b/data_structures/hashing/double_hash.py @@ -54,13 +54,13 @@ def _collision_resolution(self, key, data=None): >>> dh.keys() {10: 10, 9: 20, 8: 30} - 3. Try to add three data elements when the size is one - >>> dh = DoubleHash(2) + 3. Try to add three data elements when the size is four + >>> dh = DoubleHash(4) >>> dh.insert_data(10) >>> dh.insert_data(20) >>> dh.insert_data(30) >>> dh.keys() - {10: 10, 9: 20, 8: 30} + {9: 20, 10: 10, 8: 30} """ i = 1 new_key = self.hash_function(data)