Lab 8: Practice with Modules and Indefinite Loops
Goals
After the lab, you should be proficient at
- reading and processing data from files
- writing data to files
- reading numeric data from files
- practice with lists
Objective: Review
Review the slides for today's lab.
Objective: Set Up
- Set up the help client:
run runHelpClient &
- Copy
/csdept/courses/cs111/handouts/lab8/data
and all of its contents, recursively, into yourcs111
directory. This way, you don't have to create thelab8
directory first. Otherwise, you could first create thelab8
directory and then copy all of the contents of the/csdept/courses/cs111/handouts/lab8
directory (i.e., add a * to thecp
command) into yourlab8
directory.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file. Name the files, as usual.
Your programs will be graded on correctness, style, efficiency, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.
After you've developed a correct solution to each program, restart IDLE or close and reopen the IDLE "shell" by running the program again (using F5), demonstrate that the program works using several good test cases, and save the output to a file named lab8.x.out, where x is the problem number.
- (20) Copy your
lab7.8.py
program and modify it so that it encodes many phrases using the Caesar Cipher program. Specifically, you will add a function calledencodeFile
that takes as parameters a filename (as astr
) and a key and returns the complete encoded message, preserving new line characters.Write a function to test this function. For example, process the
data/phrases.txt
file that you copied from thehandouts
directory. We're keeping the data files in a separate directory from the Python programs, which is good practice.Then, modify the
main
function such that it prompts the user for (1) the name of a file that contains phrases that need to be encoded and (2) a key, read the file of phrases to be encoded, and output the encoded phrase.Example final output:
This program will encode a whole file using Caesar ciphers. Enter the name of a file to encode: data/song.txt Enter an encoder key (an integer between -25 and 25): 1 The encoded file is uijt pof hpft pvu up uif pof j mpwf uijt pof hpft pvu up uif pof j mfgu cfijoe
What is a simple test for this problem? Make a file for testing in your
data
directory. Try to keep it simple as you're trying to make sure your program works.Doesn't that make testing the encodings a little easier, since you don't need to type out the whole phrase every time you test?
- (15) Copy the previous program and modify it so that it writes
the encoded message to a file because, ultimately, we want to encode
whole files. The program should take as input the encoded message
output file's name. You may want to assume that the output files
always get written to the
data
directory so that you don't clutter yourlab8
directory with files.The idea is that now you can encode files that you can give to others to decode (and vice versa).
Example output:
What is the name of your file to encode? phrases.txt What is the name of the file to output the encoded file to? encoded.txt What is the key to use to encode? 12 Your message has been successfully written to data/encoded.txt
Note: I broke up the large problem of encoding files using a Caesar cipher and writing the result into another file into multiple, smaller problems, each of which is easier to tackle than tackling the whole problem all at once. This is an important problem-solving skill to develop. You may feel like breaking the problem into smaller problems slows you down, but it usually doesn't because you spend less time debugging the smaller pieces than if you had tackled it all at once. You'll get lots of practice solving problems in this class--including on the next problem!
- (20 pts) Write a program that determines if a string (input by a
user) is a palindrome. A palindrome is a word that is
the same fowards and backwards. Some example palindromes:
"kayak", "A man A plan A canal
Panama". Palindrome
List
Break the problem into at least two functions:
main
andisPalindrome
, which returns True iff the parameter string passed into the function is a palindrome. Depending on how you think about the problem, you may want to break the solution into more functions.You should remove spaces from the string before processing and consider upper and lower case letters as the same, i.e., "A man A plan A canal Panama" is a palindrome. (What string methods can you use to accomplish these tasks before processing?) However, you don't need to consider punctuation as special cases.
(Recall you wrote a function to reverse a string a few weeks ago.)
For this problem, you should strive for efficiency, but, given our current knowledge of programming building blocks, your solution may be inefficient. For example, you may require an extra string variable, or even though you already "know" that a string is not a palindrome, you may keep checking if it is a palindrome.
You can write an efficient solution for an extra 5 points. (After you've finished the other programs, talk to me about your solution if you're curious about the extra points.)
Example output:
What is your phrase? noon noon is a palindrome.
What is your phrase? noon. noon. is not a palindrome.
What is your phrase? A man a plan a canal Panama A man a plan a canal Panama is a palindrome.
What is your phrase? A man. A plan. A canal. Panama. A man. A plan. A canal. Panama. is not a palindrome.
- (35)
Break this problem into pieces (what are those pieces?) and
tackle them one at a time.
Create functions that solve each piece.
The system of scoring an Olympic gymnast is based on two separate panels of scores. The A panel judges the requirements, difficulty, and connections of a routine. The scoring starts at zero and then adds points accordingly. The B panel judges the execution of a routine, and the scoring starts at 10 with points deducted accordingly for execution and for any applicable violations such as stepping out of bounds or being over the time limit.
For each panel of judges, the lowest and highest scores are dropped (to prevent judges from biasing the results) and then averaged. The two panels are then added together for the final score. A very good score will range in the 15s and 16s.
Your task:
- Read in a file containing the gymnastics scores. (The file name can be a constant in your program.) The first line of the file is the already-averaged difficulty score. The remaining six lines are the judges' execution scores. (You cannot assume that the execution scores are sorted.)
- Calculate the average score, as described above.
- Display the judges' scores and the final score in the format
shown below.
Example input file:
5.7 8.3 9.1 8.0 8.9 8.8 8.5
Example output:
Gymnastics Scores for data/scores.dat -------------------------------------------------------- Judges Execution Scores: 8.0 8.3 8.5 8.8 8.9 9.1 Average Execution Score: 8.625 Average Difficulty Score: 5.7 The Final Score: 14.325
Your output does not need to display the scores in sorted order. However, the output should be formatted as above.
Example files are
data/nastia.dat
anddata/shawn.dat
Finishing up: What to turn in for this lab
- IDLE and jEdit may create backup files with the "~" extension. Delete these files from your lab directory to save paper when you print.
- Submit
your
lab8
directory into yourturnin
directory. - Create the printable lab assignment, using the
createPrintableLab
command:
createPrintableLab <labdirname> - View your file using the
evince &
command. - Print the file using
the
lpr
command. - Log out of your machine when you are done.
Perform the following steps from
your cs111
directory.
Note that each command
below links to a page with more information about using the
command.
Labs are due at the beginning of Friday's class. You should
hand in the printed copy at the beginning of class, and the
electronic version should be in the turnin
directory before Friday.
Ask well before the deadline if you need help turning in your assignment!
Grading (90 pts)
- Python programs: 90 pts; see above for breakdown