Complete the code to calculate the hash index for a given key.
def hash_function(key, size): return key [1] size
The modulo operator (%) is used to ensure the hash index stays within the table size.
Complete the code to find the next index in linear probing.
def linear_probe(index, size): return (index [1] 1) % size
In linear probing, we move to the next slot by adding 1 to the current index.
Fix the error in the insertion loop to handle collisions correctly.
def insert(hash_table, key, size): index = hash_function(key, size) start_index = index while hash_table[index] is not None: index = (index [1] 1) % size if index == start_index: return False # Table full hash_table[index] = key return True
We add 1 to the index to move to the next slot in linear probing.
Fill both blanks to complete the search function using linear probing.
def search(hash_table, key, size): index = hash_function(key, size) start_index = index while hash_table[index] is not None and hash_table[index] != [1]: index = (index [2] 1) % size if index == start_index: return -1 # Not found if hash_table[index] == key: return index return -1
We compare the slot with the key and move forward by adding 1 to index.
Fill all three blanks to complete the delete function using linear probing.
def delete(hash_table, key, size): index = hash_function(key, size) start_index = index while hash_table[index] is not None: if hash_table[index] == [1]: hash_table[index] = [2] # Mark as deleted return True index = (index [3] 1) % size if index == start_index: return False # Not found return False
We check if the slot has the key, mark it as 0 (deleted marker) to delete, and move forward by adding 1.
