We briefly talked about character encoding (Section 6.3), nested loops (with applications to image processing), while loops, and exceptions. We also saw tuples and dictionaries. Next week, we will briefly talk about sets and move on to recursion.
Other topics we mentioned (or I should have 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.3-5.8 (plus the ecase study on image processing), 4.4 on exceptions, and 6.1 on dictionaries. We will continue with 6.2 and then move to chapter 10.
2. [Swapping While for For, 20pt] The following two programming tasks would naturally be solved by for loops. For this problem, you want to solve them using while loops instead. No credit for solutions using for (though you may want to start by programming the for solution).
a) [7pt] Write a function acronym(s) that returns a string consisting of all the upper-case letters in s (in order that they occur in s). See below for examples. Use a while loop, but avoid infinite while loops (so no break/continue allowed).
b) [7pt] White a function N(n) which prints the letter N as ASCII art in an nxn display (we did this example in class). Do not use for loops, or string multiplication. Really use while. Again, do not use an infinite while loop (so no break/continue allowed).
c) [6pt] There is a good chance your solution to b used two while loops (did it?). If so, rewrite your program so it only uses one, single while loop (which again cannot be an infintie loop, so no break/continue allowed).
3. (Interactive Loop, 10pt) Write a function prod() that allows the user to manually enter any number of numbers, and then prints out a message stating the product. The user stops the input by either entering nothing (first example below), or hitting ctrl-c (second example below, looks the same as the first). If the user enters a non-number (for which eval does not work), the program should just state that the product is undefined (third example below). You can use a mixuter of if and exceptions to do this (only one of the cases really requires an exception: KeyboardInterrupt), but for this problem you can try handling all cases with exceptions if you want to). Hint: Infinite loop is encouraged.
4. (Voting, 10pt) Your company needs a new director of tea services. As the local IT consultant you have to write the program vote() that allows people to vote for their favorite. The program is running on a terminal, and everybody who wants to vote can go up to the terminal and enter a name. When everybody has voted, return is pressed, and a summary statistics is presented: who was voted for and how many votes did they get. (No need to order results.) See below for sample run (including incorrect grammat). Hint: you need a dictionary of counters.
5. (Extra Credit, Guessing Game, +10EC) Implement a function guess(fname) which runs the following guessing game. The funtion picks two words randomly from the text in file fname and presents them to the player. The player has to guess which word occurs more often. They are told whether they are right or not, and the actual statistics (how often each word occurs) is displayed. The game continues until the player enters nothing (return), at which point they are told how often they were right and wrong. See below for screenshots. Hint 1: use choice() from the random module to pick a random word from a list of words. Hint 2: when implementing this, you can build the game-play part and the file-reading/processing part separately.