October 2025
Intermediate to advanced
485 pages
13h 2m
English
These are the solutions to the Exercises.
Here, I’ve updated the DivisionHasher class to incorporate the Rabin-Karp initial hash function as a basic way to hash any string:
| | import random |
| | |
| | |
| | class DivisionHasher: |
| | |
| | def __init__(self, array_length): |
| | self.array_length = array_length |
| | self.base = 26 |
| | |
| | # Choose a random prime number: |
| | p = random.randint(1000, 10000) |
| | while not self.is_prime(p): |
| | p = random.randint(1000, 10000) |
| | |
| | self.prime = p |
| | |
| | def hash(self, key): |
| | key = str(key) |
| | |
| | result = self.character_hash_code(key[0]) % self.prime |
| | |
| | for i in range(1, len(key)): |
| | result = \ |
| | (result * self.base + self.character_hash_code(key[i])) \ |
| | % self.prime |
| | |
| | return result |
| | |
| | def character_hash_code ... |
Read now
Unlock full access