In Class: Week 9 Tuesday and Thursday:
Search


Create a week09 subdirectory in your cs21/class directory by running update21:

$ update21
$ cd
$ cd cs21/class/week09

Topics


In-class work

  1. Class will begin with a concept quiz to reinforce the concept of a list of lists. You should be able to interpret the use of a double bracket (e.g., ls[0][0]). The items of a list can be any type, including a list. In Python, the items within one list do not have to be the same type, so we can store both the a string (e.g., student's name) and ints (e.g., student's quiz grade) in the same list.
  2. Previously, we introduced the val in list operator, which evaluates to a boolean expression of True if val matches a value in list and False otherwise. The not in operator works similar, but returns the opposite value.
  3. Gradebook - list of lists

  4. Last week, we started working on the program to gradebook.py. Open up gradebook_wk9.py to review the program.
  5. Together, we will trace through the program and draw the stack diagram, including a call to printStudentInfo()
  6. It is important to practice defensive programming. When receiving data (through user input or parameters), you should verify the data is correct and not assume the user/caller used your program correctly. For example, printStudentInfo() should check to make sure the studentID number is valid, otherwise Python will crash.
  7. On your own, implement the changeGrade() function. The function should change a student's quiz grade to a new score. What inputs do you need? How should we make sure the arguments the function receives are valid?
  8. Linear Search

  9. We have used Python functions for searching (e.g., in, list.find()). How do these methods actually work? We will discuss the algorithm for searching a collection of items.
  10. Open up the file searchCards.py. This program creates a list of random ints and asks the user for a value to search the list for. We will write a function findCard() that takes in a deck of cards (a list of ints) and an int value to search for. Our function can return a boolean value for whether the value was found, or the actual position of the value in the list.
  11. In gradebook.py we will implement a function, findStudent(), to search the gradebook for a student's name. The function takes in the gradebook, a string containing the student's name and returns the position of the student's information in the list.

    Complexity of Search

  12. How do we analyze how long it takes for search to finish? We will count steps to analyze a programs complexity. What counts as a step? What happens in the best case? Worst case? How many steps does linear search take in the worst case?
  13. Binary Search

  14. Using playing cards, we will consider how we can make searching go faster if the list of items is in sorted order. Binary search works by using divide and conquer. Each step of the algorithm divides the number of items to search in half until it finds the value or has no items left to search.
  15. In testsearches.py, we will implement the binary search algorithm to search a list for some random value.
  16. Why is it called binary search? How many steps does it take for binary search to run? We will see that binary search is O(log N). What does this mean relative to linear search? Run testsearches.py with different values of N to see how the run time of binary search grows with increasing list sizes. How does this compare to linear search's growth?