Session 23

More Experience with Lists


CS 1510
Introduction to Computing


Code Review of Homework 9

Doc, it hurts when I do this.

Homework 9 was shorter than its predecessor, but created offered some interesting challenges. How can we print out the number of matches before printing the lines that match? How do we do the match? How do we organize the code?

A great way to improve as a programmer is to study code. There are many variations:

Let's review a program. With the program you are given, do the following:

... do a group review. What do you like? Ways to improve the code? Questions about the code? Comments?

... advice on work habits. Starting late. Asking questions. Doc, it hurts when I go like this.... Sometimes, the best advice really is, So don't go like that. Even if it leaves me open to this:

So I killed him.

How can I help you not hit yourself on the head?



Using Lists to Improve Our Code

... my solution didn't use lists. Instead, it built a string to print.

... some weaknesses. How I prepare lines and strings for matching. Embedding formatting into the processing code. search() function having to open and close the file.

Each function should do one thing.

... advantages of print_result() function. Frees search() of those details. A single place for this conceptual action. Easier to change or replace the output behavior.

... one more step issue? Create a function called search_by_filename() to handle input file open and close. What are the advantages of this step?

Look at structure of the program:

                                main
                                 |
               --------------------------------------
              |                  |                   |
          get_files      search_by_filename    print_result
                                 |
                                 |
                               search
                                 |
                                 |
                            multi_find

... decompose the problem into conceptual pieces. Write a function for each piece.



Other Assignments

Any questions about Homework 10? Please take baby steps! Some of these may take a while to run... How might you make your code faster?

Return Homework 8. We looked at my solution last week. Here is the Python file, if you'd like to study more. It is never too late to ask questions, if you have them.



Image Credits

The image of the man hitting himself on the head is available for purchase through Vecto.rs.

The cartoon of the two convicts comes from The New Yorker and is available for purchase through the Conde Nast store. (The New Yorker has published many great one-panel cartoons over the years.)

(return to code review)



Wrap Up



Eugene Wallingford ..... wallingf@cs.uni.edu ..... November 11, 2014