mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Added doctest to hash_table.py (#10984)
This commit is contained in:
parent
42c49ee117
commit
29b8ccdc2f
|
@ -21,6 +21,29 @@ class HashTable:
|
||||||
self._keys: dict = {}
|
self._keys: dict = {}
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
|
"""
|
||||||
|
The keys function returns a dictionary containing the key value pairs.
|
||||||
|
key being the index number in hash table and value being the data value.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
1. creating HashTable with size 10 and inserting 3 elements
|
||||||
|
>>> ht = HashTable(10)
|
||||||
|
>>> ht.insert_data(10)
|
||||||
|
>>> ht.insert_data(20)
|
||||||
|
>>> ht.insert_data(30)
|
||||||
|
>>> ht.keys()
|
||||||
|
{0: 10, 1: 20, 2: 30}
|
||||||
|
|
||||||
|
2. creating HashTable with size 5 and inserting 5 elements
|
||||||
|
>>> ht = HashTable(5)
|
||||||
|
>>> ht.insert_data(5)
|
||||||
|
>>> ht.insert_data(4)
|
||||||
|
>>> ht.insert_data(3)
|
||||||
|
>>> ht.insert_data(2)
|
||||||
|
>>> ht.insert_data(1)
|
||||||
|
>>> ht.keys()
|
||||||
|
{0: 5, 4: 4, 3: 3, 2: 2, 1: 1}
|
||||||
|
"""
|
||||||
return self._keys
|
return self._keys
|
||||||
|
|
||||||
def balanced_factor(self):
|
def balanced_factor(self):
|
||||||
|
@ -37,6 +60,43 @@ class HashTable:
|
||||||
print(self.values)
|
print(self.values)
|
||||||
|
|
||||||
def bulk_insert(self, values):
|
def bulk_insert(self, values):
|
||||||
|
"""
|
||||||
|
bulk_insert is used for entering more than one element at a time
|
||||||
|
in the HashTable.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
1.
|
||||||
|
>>> ht = HashTable(5)
|
||||||
|
>>> ht.bulk_insert((10,20,30))
|
||||||
|
step 1
|
||||||
|
[0, 1, 2, 3, 4]
|
||||||
|
[10, None, None, None, None]
|
||||||
|
step 2
|
||||||
|
[0, 1, 2, 3, 4]
|
||||||
|
[10, 20, None, None, None]
|
||||||
|
step 3
|
||||||
|
[0, 1, 2, 3, 4]
|
||||||
|
[10, 20, 30, None, None]
|
||||||
|
|
||||||
|
2.
|
||||||
|
>>> ht = HashTable(5)
|
||||||
|
>>> ht.bulk_insert([5,4,3,2,1])
|
||||||
|
step 1
|
||||||
|
[0, 1, 2, 3, 4]
|
||||||
|
[5, None, None, None, None]
|
||||||
|
step 2
|
||||||
|
[0, 1, 2, 3, 4]
|
||||||
|
[5, None, None, None, 4]
|
||||||
|
step 3
|
||||||
|
[0, 1, 2, 3, 4]
|
||||||
|
[5, None, None, 3, 4]
|
||||||
|
step 4
|
||||||
|
[0, 1, 2, 3, 4]
|
||||||
|
[5, None, 2, 3, 4]
|
||||||
|
step 5
|
||||||
|
[0, 1, 2, 3, 4]
|
||||||
|
[5, 1, 2, 3, 4]
|
||||||
|
"""
|
||||||
i = 1
|
i = 1
|
||||||
self.__aux_list = values
|
self.__aux_list = values
|
||||||
for value in values:
|
for value in values:
|
||||||
|
@ -69,6 +129,21 @@ class HashTable:
|
||||||
self.insert_data(value)
|
self.insert_data(value)
|
||||||
|
|
||||||
def insert_data(self, data):
|
def insert_data(self, data):
|
||||||
|
"""
|
||||||
|
insert_data is used for inserting a single element at a time in the HashTable.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
>>> ht = HashTable(3)
|
||||||
|
>>> ht.insert_data(5)
|
||||||
|
>>> ht.keys()
|
||||||
|
{2: 5}
|
||||||
|
>>> ht = HashTable(5)
|
||||||
|
>>> ht.insert_data(30)
|
||||||
|
>>> ht.insert_data(50)
|
||||||
|
>>> ht.keys()
|
||||||
|
{0: 30, 1: 50}
|
||||||
|
"""
|
||||||
key = self.hash_function(data)
|
key = self.hash_function(data)
|
||||||
|
|
||||||
if self.values[key] is None:
|
if self.values[key] is None:
|
||||||
|
@ -84,3 +159,9 @@ class HashTable:
|
||||||
else:
|
else:
|
||||||
self.rehashing()
|
self.rehashing()
|
||||||
self.insert_data(data)
|
self.insert_data(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user