Learn
Hash Maps: Python
Creating the Hashing Function

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

1.

Create a method for HashMap called .hash(). This method should take two arguments: self and key.

2.

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.

3.

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.

4.

Return hash_code.

Folder Icon

Sign up to start coding

Already have an account?