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
- List of lists
- strip() vs rstrip()
- split()
- Searching using the in and not in operators
- Linear search
- Complexity of linear search
- Big-O notation
- Binary search
- Complexity of binary search
In-class work
- 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.
- 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.
Gradebook - list of lists
- Last week, we started working on the program to gradebook.py. Open up
gradebook_wk9.py to review the program.
- Together, we will trace through the program and draw the stack diagram,
including a call to printStudentInfo()
- 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.
- 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?
Linear Search
- 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.
- 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.
- 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
- 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?
Binary Search
- 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.
- In testsearches.py, we will implement the binary search algorithm
to search a list for some random value.
- 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?
include("style/footer.php"); ?>