Lab 7: Strings, ASCII, and Lists
Goals
After the lab, you should be proficient at
- using escape sequences
- formatting strings
- using the ASCII representations of characters with built-in functions
- solving problems with lists
Objective: Review
Review the slides for today's lab.
Objective: Set Up
- Set up the help client:
runHelpClient & - Create a directory for this lab.
- Copy
test.py
fromlab6
into yourlab7
directory.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file.
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
- (10 pts) Using a
for
loop, draw a diagonal line that looks like:\ \ \ \ \
Think about the pattern of what is getting printed.
After you have that working, have the user enter the size of the diagonal line and draw a line of the appropriate size.
- (8 pts) Copy
lab1/lab1.4.py
into this directory. Update the authorship appropriately. Modify it such that the program always displays the result to 3 decimal places. Output should look similar to:This program prints the Celsius temperature given a Fahrenheit temperature. Enter a Fahrenheit temperature: 70 70 degrees F is 21.111 degrees C
- (18 pts) Write a program that creates a table of Olympic
competition running distances in meters, kilometers, yards, and
miles. The following distances should be used: 100 m, 200 m, 400
m, 800 m, and 1600m.
Note that 1 m = .001 km = 1.094 yds = .0006215 mi
Calculate and display the results, formatted in the following manner:
Meters Kilometers Yards Miles --------------------------------------- 100 0.100 109.4 0.062 200 ----- ----- ----- 400 ----- ----- ----- 800 ----- ----- ----- 1600 ----- ----- -----
Note: Make sure your output is formatted exactly as above except that you will have all the converted values filled in.
Your final solution should not have a lot of repeated code. How can you solve this problem without repeated code?
- (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 thelist
constructor (in other words, only one line of code). - Second method: create an empty list and
append
the values in a loop.
Display 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, noting the desired
casing of the 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
- (13) Write a function called
encodeLetter
that takes as parameters a lowercase letter and a key returns the letter encoded. For example, if you call the functionencodeLetter('a', 1)
, the function should return 'b'. Similarly, if you call the functionencodeLetter('b', -1)
, the function should return 'a'.As we've been doing, write the function and test it using the
test.testEqual
function in a function calledtestEncodeLetter
What are good test cases for this function?
No main or user input for this program... We're building up to that.
- (13) Copy the previous program and add a function
called
encodeMessage
that takes a message to encode and a key as parameters and returns the encoded message. The function you created in the last problem will likely be helpful to call. (Read: you should call that function in this function.) You should NOT modify the function you wrote in the previous problem. You already know that's working, so don't modify that function.Note that spaces will have to be handled specially.
As before, create a function that tests this function. What are good test cases for this function?
Still no main or user input for this program... We're building up to that.
- (13) Copy the previous program and create your final Caesar
Cipher program. The program will take as input a phrase to encode
and a key and will display the encoded phrase.
Your program should handle the error case where the
user-supplied key is not in the valid range.
Your program can assume that messages only contain lowercase letters and spaces.
Give your neighbors a message to decode, along with the original key used to encode the message. Put your neighbors' names, 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
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
Pedagogical Overview:
We're going to build up the Caesar cipher code in a couple of steps.
By the end of the term, you should feel comfortable with breaking up a
problem yourself. In the meantime, we'll go through a few examples of
breaking up a program into components that are easier to solve. You
can peek at the last problem to see where we're going.
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.
Recall that the alphabet wraps around. For example, 'z' with a key
of 5 is encoded as 'e'.
Rhetorical question: why is the limit on keys between
-25 and +25?
Extra Credit (up to 8 pts)
We can provide problem clarifications, but I'd like you to try to solve extra credit problems on your own (in your pair).
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
- When you, as a pair, are ready to submit OR if you are at the
end of the lab period, run
pairturnin.sh labx partnerusername
wherelabx
is the name of the lab you are submitting andpartnerusername
is your partner's username on the lab machines (the person whose account you are not using to write the code). For more information about the command, see the wiki.If you want to copy your pairs' work into your
cs111
directory--either just to have it or to work on your code on your own--use the scriptstartup.sh
. Run this command from the account that you want For example, run startup.sh labx partnerusername
wherelabx
is the name of the lab you're working on andpartnerusername
is your partner's username on the lab machines.For more info, see the wiki.
- If you complete the lab on your own after the lab
period, submit
your lab into your
turnin
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
class on Friday.
Ask well before the deadline if you need help turning in your assignment!
Grading (100 pts)
- Python programs: 100 pts; see above for breakdown