﻿1
00:00:00,470 --> 00:00:07,610
‫So now we'll create the hash function and this function will be used in the set function and the get

2
00:00:07,610 --> 00:00:08,360
‫function.

3
00:00:08,690 --> 00:00:14,450
‫I am going to bring up this graphic that we've been using to represent the hash function and then we'll

4
00:00:14,450 --> 00:00:16,430
‫start our code out like this.

5
00:00:16,430 --> 00:00:23,030
‫So we'll pass it a string, which is the key, and then this will return an integer, which is going

6
00:00:23,030 --> 00:00:26,600
‫to be the index where we store the key value pair.

7
00:00:26,960 --> 00:00:30,500
‫So we'll have a variable that we call hash that is an integer.

8
00:00:30,500 --> 00:00:37,190
‫We'll set that to be equal to zero to begin with, and then we'll create a for loop that will use to

9
00:00:37,190 --> 00:00:39,650
‫iterate over the key.

10
00:00:40,040 --> 00:00:48,410
‫So let's say the key is paint will iterate over these letters because it's basically an array of characters

11
00:00:48,770 --> 00:00:57,410
‫and then we'll have an integer that we call ASCII value and we'll set it equal to the key at the index

12
00:00:57,410 --> 00:01:00,770
‫of I as we're iterating over these letters.

13
00:01:00,950 --> 00:01:07,430
‫But notice that this key is a letter, but we're turning it into an integer.

14
00:01:07,790 --> 00:01:16,160
‫So to explain how we can do this, I'm going to flip over to a web browser and do a search for ASCII

15
00:01:16,160 --> 00:01:16,910
‫table.

16
00:01:17,180 --> 00:01:18,500
‫So I'll press enter.

17
00:01:18,890 --> 00:01:25,670
‫And what you can do with your search is just click on the first item returned and you'll get something

18
00:01:25,670 --> 00:01:27,560
‫that looks like this.

19
00:01:27,860 --> 00:01:33,890
‫So all of the letters that are in the key have an equivalent decimal value.

20
00:01:34,070 --> 00:01:35,810
‫So we said that the key was paint.

21
00:01:35,810 --> 00:01:37,340
‫The first letter is P.

22
00:01:37,340 --> 00:01:45,140
‫If we come down here, P has a value of 112, capital P has a different value.

23
00:01:45,650 --> 00:01:47,240
‫So now let's flip back.

24
00:01:47,880 --> 00:01:54,420
‫So this letter P, when we convert it into an integer, has the value of 112.

25
00:01:54,750 --> 00:01:58,800
‫So now we'll create our equation that we're going to use for our hash.

26
00:01:58,800 --> 00:02:09,180
‫We'll say hash equals and I'm going to set mine to be equal to hash plus ASCII value times 23.

27
00:02:09,480 --> 00:02:16,380
‫And the reason I multiply this by the number 23 is if you multiply it by a prime number, it makes the

28
00:02:16,380 --> 00:02:18,060
‫results more random.

29
00:02:18,540 --> 00:02:21,120
‫And now comes the really important part.

30
00:02:21,150 --> 00:02:23,370
‫We're going to say modulo.

31
00:02:23,490 --> 00:02:25,590
‫That's the percent sign there.

32
00:02:25,860 --> 00:02:28,410
‫And then we're going to use the size.

33
00:02:28,410 --> 00:02:32,940
‫So this is the size of data map, the address space.

34
00:02:33,090 --> 00:02:36,930
‫And we had set that size to be a constant of seven.

35
00:02:36,930 --> 00:02:43,350
‫So if you've never seen modulo before, what it does is it gives you the remainder when you divide.

36
00:02:43,530 --> 00:02:52,800
‫So if you divide by seven, the remainder can be anywhere from 0 to 6, and our address space goes zero

37
00:02:52,830 --> 00:02:54,000
‫through six.

38
00:02:54,570 --> 00:02:58,740
‫Now the only thing left to do is to return hash.

39
00:02:58,950 --> 00:03:06,030
‫And because we use this modulo operator up here with the size, this is always going to be an integer

40
00:03:06,030 --> 00:03:08,880
‫that will be zero through six.

41
00:03:09,450 --> 00:03:13,200
‫And that is our overview of the hash function.

