Skip to content

Added doctest to double_hash.py #11020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions data_structures/hashing/double_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 one
>>> dh = DoubleHash(1)

Typo

Copy link
Contributor Author

@Suyashd999 Suyashd999 Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

I tested adding elements to DoubleHash with size 1, to my surprise it resulted in getting stuck in infinite loop, where a new hash value tries to be calculated but is unable to.

For now I have changed that part of test. I'll soon be raising an issue and also will try to fix it.

>>> 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)

Expand All @@ -50,3 +77,9 @@ def _collision_resolution(self, key, data=None):
i += 1

return new_key


if __name__ == "__main__":
import doctest

doctest.testmod()