Tip:
Highlight text to annotate it
X
[Narrator] Now that we've created our empty hash table,
the next steps are to figure out how to do look-ups as well as adds.
Both look-ups and adds depend on the same first step.
We need to find the right bucket, so that's what we'll do next.
Remember the idea for a hash table.
We have a list of buckets.
Each bucket is a list of entries and each entry is a key and a list of values.
So, regardless of whether we want to do look-ups and find the value associated with a word
or we want to do add and add a new value associated with a word,
the first thing we always have to do is find the right bucket.
If you find the right bucket if we're doing a look-up the next thing we're going to do is
need to look through all the entries in that bucket to find if there's one
that matches the key word.
If we're doing add we also need to start by finding the right bucket.
Then we're going to look through and see if that word already exists.
If it doesn't exist we can add a new word, and we'll have a new entry with that word and that value.
Both of those depend on first being able to find the right bucket.
We'll do that once and be able to reuse that code in both look-up and add.
Our goal is to define a procedure, and we'll call it hash table get bucket, and it'll take 2 inputs.
It'll take a hash table and a keyword, and it will output the bucket where that keyword could occur.
We don't know yet whether that's keyword is actually on the table.
It might be somewhere in that bucket, it might not,
but the important thing that hash table get bucket should do is find the bucket.
One function that will be useful for doing that is the hash string procedure that you defined earlier,
and hash string takes 2 inputs; it takes a string, which is the keyword,
and it took a size the number of buckets and it outputs the number which is the index of that bucket.
There's a bit of a mismatch here that you'll have to think about to define hash table get bucket;
that hash string the input is the size, the number of buckets; it's a number.
The 2 inputs to hash table get bucket are just the hash table and a keyword.
You need to also figure out how to get the size of the hash table to pass into hash string,
and if you remember the structure of the hash table
you should be able to figure out how to do that.
Remember our hash table is a list of buckets so the value that we want to pass in to hash string is the size of that hash table;
the number of elements in the list of buckets that is the hash table.
See if you can define hash table get bucket,
and as a hint you can do the whole definition with a very small amount of code.
It should only take 1 line to do this.