echo $title ?>
Goals
After the lab, you should be proficient at
- using the ASCII representations of characters with built-in functions
- solving problems with lists
- reading and processing data from files
Review
Objective: Review
Review the slides for today.
Objective: Linux
Copy
/csdept/local/courses/cs111-01/handouts/lab6
and all of its contents, recursively, into your
labs
directory. This way, you don't have to
create the lab6
directory first.
Otherwise,
you could first create the lab6
directory
and then copy all of the contents of
the /csdept/local/courses/cs111-01/handouts/lab6
directory (i.e., add a * to the cp command) into
your lab6
directory.
Objective: Help Client Set Up
For the most recent version of the help client, run
java -jar /csdept/local/courses/cs111-01/shared/labhelp.jar
Or, run runHelpClient.sh &
We're going to try to use this more so you don't have to have your hand up.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file. Name the files as lab6.x.py, where x is the problem number.
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. I am getting tougher on these criteria as we develop larger programs.
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 lab6.x.out, where x is the problem number.
- (10) Write a program that uses two different techniques to create
lists of length 5 that each contain the values [1,2,3,4,5].
- First method: create the list using only
range
and the list constructor (in other words, only one line of code). - Second method: create an empty list and
append
the values in a loop.
Print out and label both lists.
- First method: create the list using only
- (15) Text Shorthand Generator. Create a text shorthand message
for a phrase using the first letters from a phrase/sentence.
Some example output:
This program reads in a phrase and produces a text shorthand. Enter a phrase: Laughing out loud Shorthand is: lol
This program reads in a phrase and produces a text shorthand. Enter a phrase: This phrase doesn't stand for anything Shorthand is: tpdsfa
(A Modern Family reference:)This program reads in a phrase and produces a text shorthand. Enter a phrase: Why the face? Shorthand is: wtf
- (20 pts) Write a program that randomly generates passwords. Your
program will generate a password that is randomly between 6 and 8
characters long. Each character in the password is either a
number, an uppercase letter, or a lowercase letter. For each
character in the password, first randomly decide if the character
is going to be a number, uppercase letter, or a lowercase letter.
Then, randomly generate a number or letter, as appropriate.
You'll need to use the ASCII values and the
chr
function. (Rhetorical: How could this program be used for evil instead of good? How does software prevent others from using this type of program for evil?)Use constants, as appropriate.
Example output:
The randomly generated password is eNu7LFaW
The randomly generated password is uEW7j9s
The randomly generated password is 7KslNy
- (35) Write a program to use a Caesar cipher to encode and decode
text messages. A Caesar cipher is a simple substitution cipher
based on the idea of shifting each letter of a text message a
fixed number (called the key) of positions in the
alphabet. You can assume that the messages contain only
lowercase letters and spaces.
Give your neighbor a message to decode, along with the original key used to encode the message. Put your neighbor's name, the original message, and the decoded message in comments in your finished program. If you did not finish when others were around, decode this message, which I generated using the key 12:
bkftaz ue yk rmhadufq bdasdmyyuzs xmzsgmsq
Your program should produce the following interaction with the user:
This program encodes and decodes Ceasar ciphers. Enter some text: i like the lions at the zoo Enter an integer key (between -25 and + 25): 1 The encoded text is: j mjlf uif mjpot bu uif app
Notice that the alphabet wraps around. The 'z' in 'zoo' became an 'a' in the encoded message. You should be able to use the same program to decode this message as follows:
This program encodes and decodes Ceasar ciphers. Enter some text: j mjlf uif mjpot bu uif app Enter an integer key (between -25 and + 25): -1 The encoded text is: i like the lions at the zoo
Note that spaces will have to be handled specially.
Rhetorical question: why is the limit on keys between -25 and +25?
What are good test cases for this program?
Suggestion: as we've been doing in class, wait until you've successfully written the encoding before you add user input. Hardcode a good test case; the test case can change as you determine that the code is working.
Finally, add appropriate error handling to your code.
- (20) Copy the previous program and modify it so that it encodes many
phrases using the Caesar Cipher program. Specifically,
your program will ask 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. For example, process the
data/phrases.txt
file that you copied from thehandouts
directory.Example 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? 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?
We're moving toward solving harder problems and, thus, writing larger programs. One of the main concepts you're learning in this course is how to solve problems. Try to break problems into smaller problems that you can solve. Solve a small piece in your program and try it out. Then, move to another subproblem and solve that, testing your code as you go and making sure it works.
Extra Credit (up to 8 pts)
Write a program that implements a Vigenere cipher. Some example output:
This program encodes Vignere ciphers. Enter some text: the eagle flies at midnight Enter a keyword: key Encoded text: dlc iyqpc jjsiq er qgnrgqlr
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.
- Copy your lab6 directory into
your
turnin
directory.
include("printing.html"); ?>
- Python programs: 100 pts; see above for breakdown
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
2:20 p.m. on Friday.
Ask well before the deadline if you need help turning in your assignment!