Книга: A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 2 (for True Epub)
Назад: 9:
Дальше: 11:

Chapter 10

These are the solutions to the .

  1. 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.

  2. 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
     
     return​ key % self.prime % self.array_length
  3. So … I’m going to cover this in the next chapter. But it’s good to get you thinking about this issue now, so you will better appreciate our discussion then!

Назад: 9:
Дальше: 11: