‫So now we'll create the hash function and this function will be used in the set function and the get
‫function.
‫I am going to bring up this graphic that we've been using to represent the hash function and then we'll
‫start our code out like this.
‫So we'll pass it a string, which is the key, and then this will return an integer, which is going
‫to be the index where we store the key value pair.
‫So we'll have a variable that we call hash that is an integer.
‫We'll set that to be equal to zero to begin with, and then we'll create a for loop that will use to
‫iterate over the key.
‫So let's say the key is paint will iterate over these letters because it's basically an array of characters
‫and then we'll have an integer that we call ASCII value and we'll set it equal to the key at the index
‫of I as we're iterating over these letters.
‫But notice that this key is a letter, but we're turning it into an integer.
‫So to explain how we can do this, I'm going to flip over to a web browser and do a search for ASCII
‫table.
‫So I'll press enter.
‫And what you can do with your search is just click on the first item returned and you'll get something
‫that looks like this.
‫So all of the letters that are in the key have an equivalent decimal value.
‫So we said that the key was paint.
‫The first letter is P.
‫If we come down here, P has a value of 112, capital P has a different value.
‫So now let's flip back.
‫So this letter P, when we convert it into an integer, has the value of 112.
‫So now we'll create our equation that we're going to use for our hash.
‫We'll say hash equals and I'm going to set mine to be equal to hash plus ASCII value times 23.
‫And the reason I multiply this by the number 23 is if you multiply it by a prime number, it makes the
‫results more random.
‫And now comes the really important part.
‫We're going to say modulo.
‫That's the percent sign there.
‫And then we're going to use the size.
‫So this is the size of data map, the address space.
‫And we had set that size to be a constant of seven.
‫So if you've never seen modulo before, what it does is it gives you the remainder when you divide.
‫So if you divide by seven, the remainder can be anywhere from 0 to 6, and our address space goes zero
‫through six.
‫Now the only thing left to do is to return hash.
‫And because we use this modulo operator up here with the size, this is always going to be an integer
‫that will be zero through six.
‫And that is our overview of the hash function.