English subtitles

← Add to Index Solution - Intro to Computer Science

Get Embed Code
5 Languages

Showing Revision 6 created 05/25/2016 by Udacity Robot.

  1. There are different ways that we can define add to index,
  2. here's one way that works.
  3. We're going to create a procedure, we'll call it add to index--
  4. and it takes our 3 inputs--the index, the keyword, and the URL--
  5. What we need to do in add to index is first find if the keyword already appears.
  6. To do that, we need to look through all of the entries in index.
  7. The natural way to do that is to use a 4-loop.
  8. We're going to define the procedure add to index--
  9. and it takes our 3 inputs, index, keyword and URL--
  10. to help keep track of what we are doing,
  11. I am going to draw a reminder of what the data structure of index is.
  12. Remember that it is a list of entries,
  13. and each entry is itself a list,
  14. where the 1st part is a keyword, and the 2nd part is a list of URLs.
  15. That's our data structure,
  16. that's going to help us figure out what to do to define add to index.
  17. The first thing we need to do
  18. is to check whether the keyword already exists in the index.
  19. If we can find it, well, then we want to modify that entry, rather than creating a new one.
  20. The natural way to do that is to use a 4-loop.
  21. We are going to loop through the elements of index.
  22. We will give each one the name entry to use in the block.
  23. This is what entry will be.
  24. The first time through the loop, the value of entry will be a reference to the list here--
  25. which is the first element of the index list.
  26. Now we need to find the keyword.
  27. The keyword is right here--that's the element at position zero of entry.
  28. We're going to test the value at position zero on entry
  29. identical to the keyword that's passed in
  30. we'll use the double-equal comparison to test that--
  31. If it is equal, then we found a match--
  32. this means we want to append the URL to the list of URLs associated with that entry.
  33. To get that list of URLs
  34. we want to find entry 1--that's the value at position 1 of entry--
  35. and we want to append that the new URL.
  36. Here we've found an entry that matches the keyword we were looking for--
  37. this means the keyword is already in the index--
  38. we've added the new URL to the URLs associated with that keyword--
  39. so we're done, we have nothing else to do.
  40. What we want to make sure, is that we don't continue and and do anything else.
  41. One approach would be to use break---that would end the loop--
  42. what we want to do instead is really end the whole procedure.
  43. If we did break, well, then we'd still have the problem of how do we deal with the case
  44. where the keyword wasn't found?
  45. Here we're just going to return--
  46. we're all done with add to index, we've added the URL it belongs.
  47. Now we need to think about what to do in the case
  48. where the keyword does not already exist in the index.
  49. If that's the case, then we get to the end of the loop without ever finding that entry.
  50. If we've got to the end of the loop,
  51. that means we did not find any entry in the index that matches the keyword,
  52. then what we want to do is add a new entry---
  53. and that new entry is going to have, as its value,
  54. a list containing 2 elements, it will be the keyword--
  55. and as the 2nd element
  56. we'll have a list containing the URLs that we found that have that keyword.
  57. So far we only have 1--the URL that was passed in to add to index.
  58. How do we do that?
  59. To add a new element to add to index we use append.
  60. We need something to pass in to append--that is the structure we want to add.
  61. This whole thing is what we want to add--
  62. so, that's a list containing the value keyword as its 1st element.
  63. As the 2nd element, its a list containing just the single URL.
  64. That's what we want to append.
  65. in the case where we didn't already find the keyword in the index.