Skip to Content
Learn
Hash Maps: Python
Open Addressing in the Setter

Now lets use our open addressing scheme in the setter for our HashMap.

Instructions

1.

Now that we have a hash function that uses the number of collisions to determine the hash code, we can update where we set a key in the event of a collision.

When we notice that the key we’re trying to set is different from the key at our hash code’s address, create a new variable called number_collisions, set that equal to 1.

2.

After defining number_collisions, create a new while loop that checks if current_array_value[0] != key.

3.

In the while loop, you want to replicate our setting logic while incrementing the number of collisions.

Call .hash() with both the key and number_collisions. Save that result into new_hash_code.

4.

Plug new_hash_code into .compressor(). Save that result into new_array_index.

5.

Check self.array at new_array_index and save the result as current_array_value. Check against the three possibilities:

  • If it’s None, save the [key, value] at self.array[new_array_index] and then return.
  • If it has a value, but the same key as key, overwrite the array at that address with [key, value] and then return.
  • If it has a value, but a different key, increment number_collisions.
Folder Icon

Sign up to start coding

Already have an account?