Skip to content

Added doctest to hash_map.py #11105

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 11 commits into from
Nov 4, 2023
Prev Previous commit
Next Next commit
Added doctest to hash_map.py
  • Loading branch information
Suyashd999 committed Oct 29, 2023
commit 5598625c0f086be489c938aa9f9711c337eacf1c
126 changes: 126 additions & 0 deletions data_structures/hashing/hash_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ def _get_next_ind(self, ind: int) -> int:
Get next index.

Implements linear open addressing.

Example 1:
>>> hm = HashMap(5)
>>> hm._get_next_ind(3)
4

Example 2:
>>> hm = HashMap(5)
>>> hm._get_next_ind(5)
1

Example 3:
>>> hm = HashMap(5)
>>> hm._get_next_ind(6)
2

Example 4:
>>> hm = HashMap(5)
>>> hm._get_next_ind(9)
0
"""
return (ind + 1) % len(self._buckets)

Expand Down Expand Up @@ -82,6 +102,16 @@ def _is_full(self) -> bool:
Return true if we have reached safe capacity.

So we need to increase the number of buckets to avoid collisions.

>>> hm = HashMap(2)
>>> hm._add_item(1,10)
>>> hm._add_item(2,20)
>>> hm._is_full()
True

>>> hm = HashMap(2)
>>> hm._is_full()
False
"""
limit = len(self._buckets) * self._capacity_factor
return len(self) >= int(limit)
Expand Down Expand Up @@ -114,17 +144,107 @@ def _iterate_buckets(self, key: KEY) -> Iterator[int]:
ind = self._get_next_ind(ind)

def _add_item(self, key: KEY, val: VAL) -> None:
"""
1. Trying to add 3 elements when size is 5
>>> hm = HashMap(5)
>>> hm._add_item(1,10)
>>> hm._add_item(2,20)
>>> hm._add_item(3,30)
>>> hm
HashMap(1: 10 ,2: 20 ,3: 30)

2. Trying to add 3 elements when size is 5
>>> hm = HashMap(5)
>>> hm._add_item(-5,10)
>>> hm._add_item(6,30)
>>> hm._add_item(-7,20)
>>> hm
HashMap(-5: 10 ,6: 30 ,-7: 20)

3. Trying to add 3 elements when size is 1
>>> hm = HashMap(1)
>>> hm._add_item(10,13.2)
>>> hm._add_item(6,5.26)
>>> hm._add_item(7,5.155)
>>> hm
HashMap(10: 13.2)

4. Trying to add element with key in floating value
>>> hm = HashMap(5)
>>> hm._add_item(1.5,10)
>>> hm
HashMap(1.5: 10)

5. Trying to add item with same key
>>> hm = HashMap(5)
>>> hm._add_item(1,10)
>>> hm._add_item(1,20)
>>> hm
HashMap(1: 20)
"""
for ind in self._iterate_buckets(key):
if self._try_set(ind, key, val):
break

def __setitem__(self, key: KEY, val: VAL) -> None:
"""
1. Changing value of item whose key is present
>>> hm = HashMap(5)
>>> hm._add_item(1,10)
>>> hm.__setitem__(1,20)
>>> hm
HashMap(1: 20)

2. Changing value of item whose key is not present
>>> hm = HashMap(5)
>>> hm._add_item(1,10)
>>> hm.__setitem__(0,20)
>>> hm
HashMap(0: 20 ,1: 10)

3. Changing value of same item multiple times
>>> hm = HashMap(5)
>>> hm._add_item(1,10)
>>> hm.__setitem__(1,20)
>>> hm.__setitem__(1,30)
>>> hm
HashMap(1: 30)
"""
if self._is_full():
self._size_up()

self._add_item(key, val)

def __delitem__(self, key: KEY) -> None:
"""
Example 1.
>>> hm = HashMap(5)
>>> hm._add_item(1,10)
>>> hm._add_item(2,20)
>>> hm._add_item(3,30)
>>> hm.__delitem__(3)
>>> hm
HashMap(1: 10 ,2: 20)

Example 2.
>>> hm = HashMap(5)
>>> hm._add_item(-5,10)
>>> hm._add_item(6,30)
>>> hm._add_item(-7,20)
>>> hm.__delitem__(-5)
>>> hm
HashMap(6: 30 ,-7: 20)

Example 3: Trying to remove non-existing item
>>> hm = HashMap(5)
>>> hm._add_item(1,10)
>>> hm._add_item(2,20)
>>> hm._add_item(3,30)
>>> hm.__delitem__(4)
Traceback (most recent call last):
...
KeyError: 4
"""
for ind in self._iterate_buckets(key):
item = self._buckets[ind]
if item is None:
Expand Down Expand Up @@ -160,3 +280,9 @@ def __repr__(self) -> str:
f"{item.key}: {item.val}" for item in self._buckets if item
)
return f"HashMap({val_string})"

if __name__ == "__main__":

import doctest

doctest.testmod()