Homework 2 (due 4/12)
CSC 401



We spent some time playing with strings, and then introduced a new data type: lists (Section 2.3). We talked about objects and conversion (Section 2.4) and how assignments (variables) work (Section 3.4). We saw various ways to change control flow in a program, including decisions (one-way, two-way, multi-way). Next week, we will see iteration (Section 3.2, functions and parameter passing (Section 3.5), and some more functionality for strings (formatted output) and other I/O (input/output) mechanisms, in particular files. (See Chapter 4.)

The catastrophic floating point error problem I mentioned in class was from the Ariane 5 mission.


Submission: The homework is due by end of day, midnight (I will not accept late homeworks). You can submit your homework through d2l into the drop-box for the 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 screenshots of both your code (program), 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) (Re)read Section 2.2 and read sections 2.3, 2.4, and 3.2 (up to page 62), 3.4 of the textbook. If you want to read ahead, complete Section 3.2, and check out Sections 3.3, 3.5 and start on chapter 4.

2. (Lists and Input/Output, 10pt) You want to write a short program that helps you calculate your average score on your homework assignments. The program should prompt you to enter a list of scores, and then print a message stating the average score, as shown in the screenshot below.

Hint: Two functions we saw in class will come in handy: eval() and sum(). Before you write the program you may want to write an expression (in the shell) that calculates the average of a list (as far as I know that function is not implemented in any Python library, but don't look for it, you can calculate the average with the tools you have).

3. (Lists and Decision, 10pt) You keep a list of your favorite colors (pick around 5 or 6). Write a program that allows the user to enter their favorite color. If that color is also one of your favorites, reply by saying so, otherwise say that you don't like the color.



Hint: Do use a list for your colors.

4. (Decisions, 10pt)  If there is a vote at a meeting, there are several possible outcomes based on the number of yes and no votes (abstains are not counted). If all the votes are yes, then the proposal passes "unanimously", if at least 2/3 of the votes are yes, then the proposal passes with "supermajority", if more than 1/2 of the votes are yes, then the proposal passes by "simple majority", and otherwise it fails. Write a program that asks the user for the number of yes and no votes (two separate prompts), and then prints the outcome of the vote ('unanimous', 'supermajority', 'simple majority', 'fail'). Hint: In a first step compute the total number of votes. Then remember if, elif, else.

5. (More Decisions, 10pt). Pig-latin is a popular way to disguise a word. It's simple: you take the first letter of the word, move it to the end, and then add 'ay'. For example, 'latin' turns into 'atinlay'. 'pig' becomes 'igpay'.

a) [5pt] Write a program that implements the basic pig-latin rule as described above. Test with some words.

b) [5pt] The simple rule above is incomplete. If the word starts with a vowel, a different rule applies: you simply add 'yay' at the end of the word, without moving the first letter to the end. So 'example' becomes 'exampleyay', or 'add' turns into 'addyay'. Write a program that implements both rules correctly, and decides which one to apply based on the first letter of the word.

 

Hint: you want one program, so you need to modify control flow based on whether the first letter is a vowel. How do you know? Use logic, or use a list of vowels to see whether the first letter belongs to the list.


Marcus Schaefer
Last updated: April 7th, 2017.