Add to Index – Intro to Computer Science


Now that we’ve decided on our data structure, hopefully it will be easy to write the code for building our index. I think you actually know enough to be able to write the procedure that adds new entry to the index. Your goal for this quiz is to– Define a procedure–we’ll call it add to index– and it takes 3 inputs. The 1st input is the index that we want to add the new keyword and URL to. Remember that we decided in the previous question– that the way to represent index is as a list where each element of the list is itself a list and each of the element list is a keyword followed by a list of the URLs where that keyword appears. The 2nd input, is a keyword–that’s just a string– that’s the word that we want to add to the index– and the 3rd input is the URL– which is a string that encodes the URL where that keyword appears. So, what add to index should do, depends on whether or not the keyword that’s passed in is already in the index. If the keyword is already in the index, we don’t want to create a new entry in the index for the keyword. We only want to have that keyword appear once– what we want to do instead is add the URL to the list of URLs already associated with that keyword. So, if the keyword is not in the index, what we need to do is add a new entry to the index, and that new entry will be a list with the keyword, that’s the new keyword, and the list the list of URLs where that keyword appears. This is the first one, since it wasn’t already in an index, so, it will be a list containing just one URL. That’s the list that we want to add to the index to represent that we found this keyword at this one URL so far. Let’s work through an example– Suppose we start by initializing an index to the empty list– we have no entries in our index yet– Then we’ll call add to index, adding a new keyword, passing an index as the 1st input, for the 2nd input we need a keyword let’s use the keyword udacity– and for the 3rd input we need the URL where that keyword appears. So, here’s what we should have– 1st we create index, we’re initializing it as an empty list, we don’t have any elements yet– then after we call that index, we’re going to add an element to the index list— we’ve mutated index–now it contains element– that element is a list containing the string udacity as the keyword– the 2nd element of that list is itself a list containing one element– which is the string “http://udacity.com” That’s the new value of index. If we call out to index again, this time we’re passing an index again– it’s the value that index refers to which is the structure– now the keyword is computing, and we’re passing in as the URL “http://acm.org” That’s going to mutate index again. We’re going to follow the rule here, the keyword is not in the index, so we should add a new entry. That means we are going to append a new entry to index. That new entry is going to have the keyword computing. As the second element we have a list, and this list will contain the URL we passed in, which is acm.org. I am going to show you one more example. In this case, we’re going to pass in a keyword that is already in the index. We’re calling out to index again–passing in the keyword udacity– which is already in the index here– and passing in a new URL, which is not in the index. What should happen this time– we’re going to mutate index, but instead of creating a new element– we’re going to mutate the element we already have– we’re going to look for the entry that matches udacity– it’s already in the index, so don’t want to add a new one. What we’re doing is following the 1st rule here– that says if the first word is already in the index– what we want to do is add new URL to the list of URLs associated with that keyword– but we don’t want to add any new entries to the index itself. Here’s what that will look like– we’re adding to the list of URLs a new entry– that’s the new value of the URL. I hope it’s clear now what add to index should do. I think you know enough to be able to define it yourself. That’s the goal for this quiz, for you to define the procedure– add to index that has this behavior- it takes the 3 inputs and adds a new keyword to the index.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *