Homework 2-8

Due November 3, 2015, 9:00 am

For this assignment, you will first figure out your answers in the Python shell. When you feel like you've arrived at the right answers, type them up in a single Python file and share it with the handin email address. For some guidance on how to do this, see the Python homework instructions.

Continuity Notice

For this homework, use the file you turned in for HW2-7. At the end of the homework, rename the file/save another copy and turn it in. You will not be graded on the work in HW2-7 twice.

Reminders

If a problem is marked as “(Independent)”, you may only discuss the problem with course staff. Otherwise, you are free to discuss the concepts that will help you solve the problems with classmates as well as course staff. However, you are never allowed to simply copy answers.
When you write your functions, remember to write down what your functions does and what the arguments mean, by commenting your code. Remember that comments start with a #.

Task 4

printConcordance() is relatively simple and makes our concordance useful. Given a word, a concordance dictionary returned by buildConcordance(), and the corresponding text, you want to print all occurrences of that word in the text with context. Here are the steps:

  1. Use the concordance (dictionary) to get a list of all the positions at which the word occurs. Now you want to loop through these positions.
  2. For each position:
    1. Modify the header comment to include the input/output description you thought of earlier.
    2. Form a string that spans the position. For example, it could span 20 characters of either side of where the word begins. If your word starts at position 584, then text[564:604] ought to give you reasonable context surrounding that occurrence of the word. How many characters you want to print before and after the word is your choice (anywhere between 10 and 50 is probably reasonable). Hint: You will have access to the position of each word using a variable (probably your iterator variable in a for-loop), so the endpoints of the range "564:604" in the above example would be computed by adding and subtracting some fixed amount from your variable.
    3. Print that string.
  3. Return nothing! We're just printing parts of the text using the concordance.

Task 5

Now, we'll test to make sure our print function for the concordance is working properly. The starter code has a defined variable, test_text, that contains some of Moby Dick. You can use this to test your function.

  1. Fill in the test function testPrintConcordance(). Include the input/output portion of the header string. Provide at least five test cases, and try tricky cases. Since printConcordance() doesn't actually return anything, there's no way to programmatically test whether each call does the right thing or not. Instead, just make each call and, after each one, print what you would expect to see printed. Remember to try tricky cases; that's the point of a test function.
  2. Try out this call in the interactive interpreter:
    printConcordance('ishmael', conc, test_text)
    Chances are that this function call will do something unexpected. What is going on?
    1. In a comment in the test function, explain what went wrong.
    2. Then fix the problem that you find by modifying your code in printConcordance(). Hint: You have to do some extra work when calculating the start and end indices of your strings. Make sure that no index is smaller than 0 or larger than the size of your text.

Task 6 (Extra Credit)

Run your program again, but this time, read the Moby Dick text file and print some examples from the concordance of this entire text. You have written code that reads in text files like Moby Dick already, so incorporate that. You should add this code at the bottom of your HW2-8.py file, and assign your Moby Dick to concordance to a variable.

Handin

Rename your program FirstLast_HW2-8.py and share it with cs0931handinfall2015@gmail.com .

Note: Before you turn in your Python files, make sure they run properly (save your Python file, then select Run > Run Module or hit F5 on your keyboard)! If nothing appears in the Shell, don't worry, as long as no red error messages appear. If they don't run, i.e. if red stuff starts appearing in the shell, points will be taken off!