After the lab, you should be proficient at
Copy wheeloffortune.py
and fines.py
from /home/courses/cs111/handouts/lab6/
We'll practice writing several Python programs, each in their own text file. Name the files lab6.1.py through lab6.6.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 lab6.x.out, where x is the problem number.
.py
extension and our HTML files with
the .html
extension. Using help(str)
, look
at how to use the rfind
method and compare with how
the find
method is used. Use the rfind
method to determine a file's type. Example output:
What is the name of your file? index.html That is a html file. What is the name of your file? picture.jpg That is a jpg file. What is the name of your file? 16.functions.pdf That is a pdf file.
In your algorithm, remove spaces from the string. You should 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 this 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 will 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. (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 5: ymj lnfsyx bts
Your program should produce the following sort of 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 result 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 result 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?
lab6.5.py
and
modify it so that the printing of the header is in a function. Call
the function you defined in the program. The output of this program
should be the same as the old program. Write an appropriate comment
for the function you defined.
lab6.6.py
. Then, define a function that takes as
parameters the speed limit and the clocked speed and returns the
computed fine. (What should the calculated fine be if the person
wasn't speeding?) The driver of your program will print an
appropriate message based on the returned fine. Write an appropriate
comment for the function you defined.
Then, put the driver part of the
program into a
main
function.
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.
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!