Homework 3 (due 10/8)
CSC 243

We talked more about file input/output (Section 4.3),  the log function, and various loop patterns (Section 5.2), up to nested loops. Next, we will see how to use nested loops to process images (see the case study for Chapter 5), work with nested lists (5.3) and cover the while loop (5.4).

We also mentioned:

Submission: The homework is due by midnight (I will not accept late homeworks). You can submit your homework through d2l into the drop-box for this homework.

Please prepare your homework as a single file containing all answers (e.g. doc, docx, or pdf, not a zip file). When submitting programs, include the programs, together with screenshots of test-runs of your program (make sure screenshots are sized so the text is legible; resize and/or crop the images). For an example, see hwexample.docx. How to take screenshots? Check out screenshots for MAC, Windows, Linux. Unless I ask for it specifically, you do not have to submit separate files with program code or executables. If multiple files are necessary, please do not zip them up, just submit them separately.

1. (Reading Assignment) Read Sections 5.1-5.3. If you want to read ahead, check out the rest of Chapter 5 and 4.4. After that, we will move on to Chapter 6.

2. (Crossword Puzzles, 15pt) You have a friend who likes crossword puzzles, but is not a computer scientist. As a birthday present you want to create a program that helps your fiend with filling out a crossword puzzle.  (If you don't remember crossword puzzles, you can try playing one.)

a) [8pt] Sometimes you have the first few letters of a word, in that case you can easily cheat and look for matches in a dictionary. But what happens if you only have the last few letters of a word?  Implement a fuction endswith(post) which lists all words ending in post. As a dictionary, use words.txt which contains a list of words, one per line. Below is sample output, note that the introductory text correctly distinguishes how many solutions there are.

Hint: This problem contains a couple of embellishments (correct grammar depending on number of matches, counting the number of matches, etc.). In a first step, ignore the embellishments, and just print the matching words. To see whether a word ends in a particular string you can use the .endswith string method. There's a good chance (depending on how you implement the check) that in the first run  you won't find any matches. In that case investigate why that is.

b) [5pt] That's nice, your friend says, but typically you don't have letters at beginning or end, but irregularly. So your friend wants to specify a pattern, let's say something like '*e*lo' where the asterisks are wildcard characters matching any letter. Implement a function matches(word, pattern) which returns True if and only if the word matches the pattern (with wildcards). See below for some test-runs. Note that the wildcard matches exactly one letter, not multiple letters.

c) [2pt] Write a function match(pattern) which takes a pattern and lists all words matching the pattern. (This will be a small modification of your code in a). Hand this in separately, though. See test-runs below.

3. (Looking for Words, 10pt) You're trying to remember some memorable quote from a memorable book you read, but you can't. All you can remember is that the quote contained a specific word. Write a function extend(word, fname) which looks through the text in fname, and when it finds word, finds the context of the word. For our purposes that's the five words before and the four words after it. All contexts should be written to a file with a name containing fname (the file you searched). Below are testruns on innocents.txt and sawyer.txt

The resutling files are occursinnocents.txt and occurssawyer.txt.

Hint: We've seen two ways to go from the text to a list of words. Using .split() or using the re.split() method. Either one is fine (I used .split for the testruns above, that's why the punctuation is still there). The disadvantage of split() is that you won't find all occurrences of a word, since punctuation remains after the split; the disadvantage of re.split() is that after using it all punctuation is gone, so when you're joining words back together, you've lost punctuation. We are ok with this.

4. (Ascii Art, 15pt) Write functions N(n), X(n), and P(n) to create ASCII N, X, and P letters, see screenshots below. Note that  behavior for even and odd n may differ slightly (because of n//2).


Marcus Schaefer
Last updated: October 2nd, 2019.