I'm currently doing hashing in my class. I need to create a double hashing function which takes a list and uses double hashing and returns a new list.
I understand how a list uses double hashing but I have trouble writing down the code for it.
hashkey = key % len(list)
steps = q - (key % q)
new_hashkey = steps + hashkey
#q = double_hash_value
This is the double hashing function that I have learned in class. I just have trouble implementing it in the code.
This is my attempt so far:
def double_hashing(keys, hashtable_size, double_hash_value):
hashtable_list = [None] * hashtable_size
for i in range(len(keys)):
hashkey = keys[i] % hashtable_size
if hashtable_list[hashkey] is None:
hashtable_list[hashkey] = keys[i]
else:
new_hashkey = hashkey
while hashtable_list[new_hashkey] is not None:
steps = double_hash_value - (keys[i] % double_hash_value)
new_hashkey = hashkey + steps
hashtable_list[new_hashkey] = keys[i]
return hashtable_list
values = [26, 54, 94, 17, 31, 77, 44, 51]
double = double_hashing(values, 13, 5)
print('Double =', double)
I'm fairly sure this is close to being right but I'm just making a stupid mistake or something. I understand how double hashing works but this code above does not work.
The output for this should be:
[26, None, 54, 94, 17, 31, 44, 51, None, None, None, None, 77]
but my output is:
[26, None, 54, 94, 17, 31, 44, None, None, None, None, None, 77]
The value 51 in index position is not being added.
Any help is appreciated. Thank you.