At this point, we have our building blocks, so let's go ahead and implement the set method. The method will:

  1. take the key passed
  2. run it through the hash function, and
  3. set the value in our storage at that particular index

Notice the way we're storing it as well: each index in this._storage (this._storage[idx]) is itself an array, thereby primitively solving for the collision problem.

1# Optional Node class
2class Node:
3    def __init__(self, key, value):
4        self.key = key
5        self.value = value
6
7# For inclusion in the Hashmap class
8def set(self, key, val):
9    # We can optionally set the value as a Node
10    p = Node(key, val)
11    key_hash = self.hashStr(key)
12    index = self.position(key_hash)
13
14    if not self.storage[index]:
15        self.storage[index] = [p]
16        self.size += 1
17    else:
18        list_at_index = self.storage[index]
19        if p not in list_at_index:
20            self.storage[index] = [p]
21            self.size += 1
22        else:
23            for i in self.storage[index]:
24                if i == p:
25                    i.value = val
26                    break