-
Title:
Add to Index Solution - Intro to Computer Science
-
Description:
-
There are different ways that we can define add to index,
-
here's one way that works.
-
We're going to create a procedure, we'll call it add to index--
-
and it takes our 3 inputs--the index, the keyword, and the URL--
-
What we need to do in add to index is first find if the keyword already appears.
-
To do that, we need to look through all of the entries in index.
-
The natural way to do that is to use a 4-loop.
-
We're going to define the procedure add to index--
-
and it takes our 3 inputs, index, keyword and URL--
-
to help keep track of what we are doing,
-
I am going to draw a reminder of what the data structure of index is.
-
Remember that it is a list of entries,
-
and each entry is itself a list,
-
where the 1st part is a keyword, and the 2nd part is a list of URLs.
-
That's our data structure,
-
that's going to help us figure out what to do to define add to index.
-
The first thing we need to do
-
is to check whether the keyword already exists in the index.
-
If we can find it, well, then we want to modify that entry, rather than creating a new one.
-
The natural way to do that is to use a 4-loop.
-
We are going to loop through the elements of index.
-
We will give each one the name entry to use in the block.
-
This is what entry will be.
-
The first time through the loop, the value of entry will be a reference to the list here--
-
which is the first element of the index list.
-
Now we need to find the keyword.
-
The keyword is right here--that's the element at position zero of entry.
-
We're going to test the value at position zero on entry
-
identical to the keyword that's passed in
-
we'll use the double-equal comparison to test that--
-
If it is equal, then we found a match--
-
this means we want to append the URL to the list of URLs associated with that entry.
-
To get that list of URLs
-
we want to find entry 1--that's the value at position 1 of entry--
-
and we want to append that the new URL.
-
Here we've found an entry that matches the keyword we were looking for--
-
this means the keyword is already in the index--
-
we've added the new URL to the URLs associated with that keyword--
-
so we're done, we have nothing else to do.
-
What we want to make sure, is that we don't continue and and do anything else.
-
One approach would be to use break---that would end the loop--
-
what we want to do instead is really end the whole procedure.
-
If we did break, well, then we'd still have the problem of how do we deal with the case
-
where the keyword wasn't found?
-
Here we're just going to return--
-
we're all done with add to index, we've added the URL it belongs.
-
Now we need to think about what to do in the case
-
where the keyword does not already exist in the index.
-
If that's the case, then we get to the end of the loop without ever finding that entry.
-
If we've got to the end of the loop,
-
that means we did not find any entry in the index that matches the keyword,
-
then what we want to do is add a new entry---
-
and that new entry is going to have, as its value,
-
a list containing 2 elements, it will be the keyword--
-
and as the 2nd element
-
we'll have a list containing the URLs that we found that have that keyword.
-
So far we only have 1--the URL that was passed in to add to index.
-
How do we do that?
-
To add a new element to add to index we use append.
-
We need something to pass in to append--that is the structure we want to add.
-
This whole thing is what we want to add--
-
so, that's a list containing the value keyword as its 1st element.
-
As the 2nd element, its a list containing just the single URL.
-
That's what we want to append.
-
in the case where we didn't already find the keyword in the index.