After the lab, you should be proficient at
As usual, create a directory for the programs and output you develop in this lab.
We'll practice writing several Python programs, each in their own text file. Name the files lab5.1.py through lab5.5.py.
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 lab5.x.out, where x is the problem number.
Enter the first word: zebra Enter the second word: monkey Enter the third word: pig The alphabetically first word is monkey
Enter the first word: dog Enter the second word: black cat Enter the third word: 13 The alphabetically first word is 13
Enter the first word: black Enter the second word: black cat Enter the third word: blacky The alphabetically first word is black
.py
extension and our HTML files
with the .html
extension.
Using help(str)
from the Python interpreter, look at
how to use the rfind
method and compare with how
the find
method is used. Use the rfind
method to determine any file's type based on its name.
Example output:
What is the name of your file? index.html index.html is a html file.
What is the name of your file? picture.jpg picture.jpg is a jpg file.
What is the name of your file? 15.strings.pdf 15.strings.pdf is a pdf file.
What is your string? The Beatles Your string in reverse is seltaeB ehT
What is your string? Help! Your string in reverse is !pleH
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.
You may want to have a variable that keeps track of if the string is a palindrome. (What type of variable would be most suited for that task, given that a string is either a palindrome or it isn't?)
For this problem, you should strive for efficiency, but, given our current knowledge of programming building blocks, your solution may be inefficient, i.e., even though you already "know" that a string is not a palindrome, you may keep checking if it is. 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.
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
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. If you
did not finish when others were around, decode this message,
which I generated using the key 12: yaowoazhqzfuaz otaeq dayzqk
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?
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
turnin
directory.
(Review the UNIX handout if you don't
remember how to do that.)
printLab.sh
command. You should probably print from the
labs
directory.
Before you print, view the file to make sure it's not too long
or has weird characters in it from the .pyc
file. One
command to view the file is gv lab5.ps
Print the file using the lpr
command introduced in the
first lab.
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 1:20
p.m. on Friday.
Ask well before the deadline if you need help turning in your assignment!