CS 202 Fall 2011: Assignment 9
Homework Assignment #9 : Due Monday Nov 19th before 11pm
This homework will exclusively involve programming in Python with an emphasis on Strings, Lists, and File Input. As in your previous assignment, you will be basically translating Scratch scripts into Python scripts or implementing very well-defined specifications in Python. The amount of Python code you will need to write is very, very small.You already have experience with String and Lists. Strings and Lists have a lot of similarity with one another: one can view a String as simply a List of characters. The primary difference between Strings and Lists in Python from Scratch is that Strings and Lists start with element 0 in Python and 1 in Scratch.
Part A: Strings
Your first Python program will manipulate strings. All of the information you need about strings should be in Chapter 8 of the Think Python tutorial.
Your Python program should perform the following four tasks:
- After getting a word of input from the user (i.e., a string), your program should use a while (or for) loop to print out each of the letters of the word. Just remember that strings in Python start with element 0!
- Your program should then use another loop to print out each of the letters of the (same) word in reverse order!
- Make a new variable that is the original word in reverse and print that variable. You can do this very easily in Python with a "string slice". Hint: use a step size of -1 (the third index) to go through the word backwards.
- Ask the user for a letter to count. Use another loop to count how many times that letter appears in the original word. Print out this count.
To make these four tasks very clear, they have been implemented in the Scratch script shown below and the Scratch program is available here.
Part B: Lists and Files
Your second Python program will be similar to the Candy Trader Scratch program you wrote back in Homework 6. However, Python contains some functionality which actually makes this much easier to implement in Python than in Scratch. In particular, Python contains a data structure called a "dictionary" that helps the programmmer construct histograms very easily.To learn how to read from a file, access a list, and construct a histogram, you will want to read Chapters 9, 10, and 11 (just up through 11.2)in the Think Python tutorial.
Your Python program should perform the following tasks:
- Open up a file containing a list of candy. As your input file, you should use this file containing 100 items of candy. (This is the same list you used in Homework 6.)
- Print each of the lines of the file, stripping off whitespace.
- Construct a histogram for the types of candy by using a dictionary. Hint: Section 11.1 shows how to create a histogram in 7 lines of code! Print this histogram out for the user. (Hint: you can print the histogram with a single command!)
- Repeatedly ask the user what type of candy they would like to trade for one apple. The program should take away 5 pieces of this candy and add 1 apple; you can do this by simply modifying the dictionary histogram. After each trade attempt, your code should print the whole histogram and print a message for the four different possible cases: the trade was successful, the user did not have any of that candy to trade, the user did not have enough of that candy to trade, or they tried to trade Apples for an Apple. The user specifies that they are done trading candy by typing in the word "DONE"; at this point, you should print out the histogram again.
After completing this homework, you will know enough Python that you can legitimately tell the world that you know how to program in Python! In your future, you may come across a situation where you need to automate some calculations after reading data from a file, and you can now do those tasks in Python!
Menu
Fall 2012Time: TuTh 9:30-10:45
Room: 1325 CS
Lab: 1370 CS (1st floor)
Instructor:
Prof Andrea Arpaci-Dusseau
Office Hours
TuTh 10:45-12:00
Office:
7375 Computer Sciences
Email: dusseau "at" cs.wisc.edu
Teaching Assistant:
Benjamin Bramble
Lab Hours (CS 1370)
Wed 2:00-4:00
Teaching Assistant:
Sharad Punuganti
Lab Hours (CS 1370)
Thu 1:30-3:30