Lab 11: Binary Search, Key Functions, 2D Lists
Goals
After the lab, you should be proficient at
- adapting binary search for use in another algorithm
- key functions used in sorting
- creating and processing 2D lists
Review
Review the slides for today.
Linux
Copy
the entire
directory /csdept/local/courses/cs111-01/handouts/lab11
into your cs111
directory.
Copy the files from your lab10
directory into the lab11
directory so that
you can extend your FaceSpace program in this lab.
How Big Is My Program? (10 pts)
You've been writing code for 11 weeks (or so), and the programs have slowly gotten larger. Last week's lab was the largest yet.
- From your
lab11
directory in the terminal, execute the command:wc ../lab10/*.py
wc
stands for "Word Count". We called the "word count" command on all the *.py files (i.e., the Python scripts, not other files or directories) in yourlab10
directory. The first column of output is the number of lines, the second is the number of words, and the third is the number of characters in each file. The total for all the files is listed in the last line of output.For example:
161 572 4855 ../lab10/facespace.py 122 289 2828 ../lab10/person.py 265 773 7933 ../lab10/social.py 548 1634 15616 total
- Run the
wc
command so that you only see the number of lines, using the command-line argument "-l
", i.e.,wc -l ../lab10/*.py
- Finally, write the output of the command into a file, using
the
>
operator, i.e.,wc -l ../lab10/*.py > mywc.txt
- View the output of the command that was saved in
the
mywc.txt
file, using the Unix commandsmore
orcat
or opening the file in a text editor, such as jEdit. We will come back to this file later.
Objective: Programming in Python
Extending the Social Network Program
If you haven't completed the Lab 10 social network program and you
don't expect to finish it, you can use my solution, which is in the
/csdept/local/courses/cs111-01/handouts/lab11/solution/
You can also compare your solution to mine.
Add a key
function to SocialNetwork module (10
pts)
Create a function that you will later use to sort a list of
Person
objects by their names--lowercased. We saw an
example of creating and using a key function in the
Card
class on Friday. Your function should take as a parameter a
Person
object and return the Person's name, lowercased.
As always, test your function.
Adding Search Functionality to your SocialNetwork class (30 pts)
As discussed in class, you will update
your SocialNetwork
class to include a method that finds
all the people that have a certain name. The method will take as
a parameter the name to search for and returns a list of
people who have that name. (Note, the method does not take as a
parameter the list of Person objects. Where should that list come
from?) Your method should:
- Lowercase the given name to search for to get the most intuitive results. (Save the result in a variable so you don't have to keep performing these operations on the name.)
- Use binary search to find a person that has that name.
Recall: What does binary search require of the list it searches?
If the given key is lowercased, what do you need to do to thePerson
's name? Do you have anything that you can reuse to simplify/reduce your code? - After finding a person that has that name, look around his/her
neighbors and get the rest of the people that also have that name.
(We talked about this process in class.) Add all of the
Person
s who match the name to a list. - Sort the resulting list by id. (How can you do that easily?)
- Return the list of
Person
s that have that name. - How should you represent that no one was found who had that name?
What are good test cases for this method? I have some duplicate names in the hollywood.txt file. You may need to update the test files so that they have more than one person with the same name.
Your user interface will be responsible for printing out the people that belong to the network or printing out a message if no people match.
Recommended Development Process:
By now, you should know how to do this yourself--we've been breaking down larger problems into small problems all term.
- Implement the method to just find one person with the network, exactly, and return that Person; test.
- Modify the method to handle the lowercase name; test.
- Modify the method to find *all* Persons with the name, lowercased; test.
Adding Options to your FaceSpace User Interface Program (15 pts)
Add an option that allows the user to find all the people that belong to some network. This shouldn't be too difficult if you've organized your code well.
Example Run:
ID Name Num Friends --------------------------------------------- 1 John Doe 3 2 Jane Smith 2 3 James Jones 2 4 Henry VIII 2 5 Lady MacBeth 1 6 John Doe 2 Select one of the following options: (D)isplay the social network (V)iew a person in the social network (S)earch for people in the social network Add (P)eople to the social network from a file Add (C)onnections to the social network from a file Add a (U)ser to the social network Add a pair of (F)riends to the social network E(X)port the social network (Q)uit the program Which option do you want? s What is the name of the person you want to find? jane smith 1: Jane Smith has 2 friends ... Which option do you want? s What is the name of the person you want to find? jOhN DoE 1: John Doe has 3 friends 6: John Doe has 2 friends ... Which option do you want? s What is the name of the person you want to find? Lady No person named Lady found.
Demonstrate Your Program
Demonstrate that your program works in IDLE (just the new functionality) and save the output in an appropriately named ".out" file.
Linux: How Much Code Did You Add?
Run the wc
command again. This time, view
the
size of the .py files in your lab11 directory. Add the output from this
command to your mywc.txt
file using the
>>
operator. An example of
running wc
from your lab11 directory
is wc -l *.py >> mywc.txt
Now, when you view your mywc.txt
file,
you should be able to compute how many more lines of code you added to
your files for this part of the project. Document this difference in
comments in social.py
.
Working with 2D Lists
For each of the following problems, create a new Python script.
After you've developed a correct solution to each program, restart IDLE or close and reopen the IDLE "shell" by running the program again (using F5), demonstrate that the program works using several good test cases, and save the output to a file named lab11.x.out, where x is the problem number.
- (10 pts) Create a 2D list that looks like:
[ [0,1,2], [1,3,5], [2,5,8] ]
Simply print the list; do not worry about formatting yet.
To make each row, you can either use the
range
function orappend
to a list, but do not create the list by typingtwod=[ [0,1,2], [1,3,5], [2,5,8] ]
- (10 pts) Copy the previous program for this program. Add a
function that takes a two-dimensional list as a parameter and prints
that list so that it looks like the below format. Test on the list
you created in the last program. However, your function should
properly handle any two-dimensional list.
0 1 2 1 3 5 2 5 8
- (15 pts) If you haven't already, copy all the .py files
(
connectfour.py
andcsplot.py
) from/csdept/local/courses/cs111-01/handouts/lab11
.- Open up
connectfour.py
and look at theConnectFour
class. Familiarize yourself with its attributes and how other methods we discussed are implemented. - Uncomment the
makeMove
method and its docstring comment and implement the method. Adhere to the method's comment description when implementing the method. - Uncomment the code in the
play
method, as described in the code. Runconnectfour.py
to test your game.
You should only have to modify
connectfour.py
--notcsplot.py
. - Open up
You have to hit Control-C in the terminal to kill the game. Attempting to close the window does not work.
You won't submit any output from this program.
Extra Credit: ConnectFour
Implement the _isDraw
(up to 5 pts)
or_isWon
methods (up to 10 pts) of the ConnectFour class. You can receive partial
credit.
Alternatively, you can improve the _computerMakeMove
method that chooses the "best" column to place the checker (up to 10
pts). A naive first approach (which is currently implemented) is to
randomly pick a column. A more sophisticated approach is to assign a
score to each column that says how "good" a column is for you and
select the column with the highest score. What properties would make
the column's score increase?
Finishing up: What to turn in for this lab
- Copy
your
lab11
directory into yourturnin
directory. - IDLE and jEdit may create backup files with the "~" extension. Delete these files from your lab directory to save paper when you print.
- Move
csplot.py
into the parent directory. (This is a large file that you don't want to print out.) - Remove any .pyc files from the directory; otherwise, you'll get an error when creating the output file. include("printing.html"); ?>
Labs are due at the beginning of Friday's class. You should hand
in the printed copy at the beginning of class, and the electronic
version should be in the turnin
directory before 2:25
p.m. on Friday.
Ask well before the deadline if you need help turning in your assignment!
Grading (100 pts)
- Python programs: 90 pts; see above for breakdown
- UNIX practice: 10 pts (
mywc.txt
file submitted, comments in social.py)