Homework 4 (due 4/26)
CSC 401



We reviewed some material on strings and the format function (Sections 4.1, 4.2), briefly talked about errors, exceptions and debugging (Section 4.4), and saw how to write to a file (Section 4.3). We went on to discuss various execution control structures more systematically. This included one/two/multiway decisions (Section 5.1), and iteration patterns (Section 5.2). Next week, we will complete Chapter 5, discussing two-dimensional lists and nested loops (Section 5.3), and additional loop patterns, in particular the while loop.


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


1. (Reading Assignment) Read Section 4.4 (reread Sections 4.1/4.2 if necessary), and 5.1-5.2 of the textbook. If you want to read ahead, read 5.3, and 5.4.

 2. (File Output, 10pt) Your goal in the problem is to write a function month(fname) that counts how often each month is mentioned in the textfile with name fname, and then write the result to a logfile with name fname_monthcount.txt (where fname is replaced by the atual filename). For example, I ran month('innocents.txt'), and it created the following text-file (you can see the name of the file at the top). Use a loop for the writing, do not create 12 separate write statements.

Note: To get the list of months, you can use month_name in the calendar module. To get the actualy names, use list(month_name), and skip[ the first entry (which is the empty string). Hint: Use count on the full text (no need to split, though you can), and don't worry about case (so case-sensitive is fine here). You can also test your program on the other texts: innocents.txt, sawyer.txt, roughing.txt, stranger.txt. For the filename: initially hardcode a name like 'monthcount.txt', and then think about how to include fname in the name of the file you write.

3. (Password, 10pt) Your security manager has just come up with a new rule to keep your passwords more secure: no digit may be followed by another digit. So pa12ssword, or secret999 are not legal passwords. On the other hand, hello, or 1a2b3c## are alllowed (so you are allowed to double other symbols than digits). You are responsible for implementing a function check_pwd(p) which takes p, a string containing the password, and returns True if and only if p satisfies the new anti-double digit rule (check_pwd need not check any other restrictions, like minimum length, etc.).

 

Hint: Use isdigit() will come in handy.

4. (Dropping lowest scores, 10pt) You want to implement a function drop(lst, k) that allows you to calculate the average homework score under the rule that you drop the k lowest scores. You can assume that each homework is worth 100 points. Do not use sort() or sorted() or any other sorting function in your solution. Below are some sample runs:

 

E.g. k = 0 means no homework is dropped, so 78.375 is simply the average of the scores. k = 1 means that the lowest score, in this case 0, is dropped, and the average of the remaining items is calculated. Similarly, k = 2 means 0 and 76 are dropped before the calculation of the average. Hint: Start by writing expressions for k = 0 and k = 1 explicititly (in the shell). Then try k = 2. What's the pattern that keeps repeating for which you want to use a loop? If you find yourself in a position where you want to remove an item from a list you can do so by using lst.remove(item). Note that I used lst = lst[:] at the beginning of my drop function, so that changing the list in drop does not affect the list scores.

5. (CSV, 10pt) In class we worked with the file GMATlarge.csv, which contains fake SAT scores by last name (yeah, the name is wrong, I know). Write a function score_at_least(lower_bound) which counts how many test-takers scored at least a score of lower_bound on the exam, and returns the result.

 

Hint: start with the score() function we wrote in class, and start modifying it; the underlying structure is similar, but you need to add some additional code.

6. (Extra Credit: A Word Puzzle, 10pt). NPR recently suggested the following puzzle challenge: find words which satisfy the following rule: if you consider each letter as a number according to the correspondence a -> 1, b -> 2, c -> 3, ..., z -> 26, then the value of the first letter is the sum of the values of the remaining letters in the word. For example, cab: c, which is 3, is the sum of 1 and 2 (a and b). Or here's a longer one: jade (j is 10, which is 1 + 4 + 5). What's the longest word in English you can find satisfying this rule. Use Python to find all English words satisfying this rule. Note: you can use words.txt, which is a---probably incomplete and inaccurate---list of English words adapted from a list I found on the web.



Marcus Schaefer
Last updated: April 21st, 2017.