October 2025
Intermediate to advanced
485 pages
13h 2m
English
These are the solutions to the Exercises.
We can feed the app integers that, when divided by 997, each produce the same remainder. For example, we can feed the app numbers that produce a remainder of 0, such as 1994, 2991, 3972, and so on. This will cause all these integers to end up in the same hash table slot, namely, index 0. If all the integers we fed the app would have the remainder of, say, 88, then all the integers would wind up at index 88 of the hash table.
Here’s one way to hash strings while also incorporating randomization:
| | def hash(self, key): |
| | # If key is a string: |
| | if isinstance(key, str): |
| | numeric = 1 |
| | |
| | |
| | for char in key: |
| | numeric *= ord(char) |
| | |
| | return numeric % self.prime % self.array_length ... |
Read now
Unlock full access