The necessary ingredient in the hash map recipe is the hashing function. A hashing function takes a key and returns an index into the underlying array.
Hash functions need to be fast to compute so that access and retrieval can be done fast.
Instructions
Create a method for HashMap
called .hash()
. This method should take two arguments: self
and key
.
Turn the key
into a list of bytes by calling key.encode()
. Save this into a variable called key_bytes
.
.encode()
is a string method that converts a string into its corresponding bytes, a list-like object with the numerical representation of each character in the string.
Turn the bytes object into a hash code by calling sum()
on key_bytes
. Save the result from that into a variable called hash_code
.
Return hash_code
.