Skip to main content.

Lab 7: Strings, ASCII, and Lists

Goals

After the lab, you should be proficient at

  1. using escape sequences
  2. formatting strings
  3. using the ASCII representations of characters with built-in functions
  4. solving problems with lists

Objective: Review

Review the slides for today's lab.

Objective: Set Up

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 good test cases, and save the output to a file named lab7.x.out, where x is the problem number.

    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 Caesar Cipher Code 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?

  1. (13) Write a function called encodeLetter that takes as parameters a lowercase letter and a key and returns the letter encoded. For example, if you call the function encodeLetter('a', 1), the function should return 'b'. Similarly, if you call the function encodeLetter('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 called testEncodeLetter

    What are good test cases for this function?

    No main or user input for this program... We're building up to that. No output for this program, unless you want me to see a bunch of "Pass" messages.

  2. (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. For example, if you call encodeMessage("w and l", 1), the function should return "x boe m".

    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. No output, unless you want to show all of your test cases pass.

  3. (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 
    
  4. (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.

  5. (8 pts) Copy lab1/lab1.4.py into this directory and name it appropriately for this lab. 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
    
  6. (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?

  7. (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.

    Display and label both lists.

  8. (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
    

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. Name the Python file lab7.ec.py. You should be able to leverage your previous code to solve this problem. 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

  1. 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
    where labx is the name of the lab you are submitting and partnerusername 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 script indiv_startup.sh. Run this command from the account that you want. For example, run indiv_startup.sh labx partnerusername
    where labx is the name of the lab you're working on and partnerusername is your partner's username on the lab machines.

    For more info, see the wiki.

  2. If you complete the lab on your own after the lab period, submit your lab into your turnin directory, as we had before this lab.
  3. Perform the following steps from your cs111 directory.

    Note that each command below links to a page with more information about using the command.

  4. Create the printable lab assignment, using the createPrintableLab command:
    createPrintableLab <labdirname>
  5. View your file using the evince command.
  6. Print the file using the lpr command.
  7. Log out of your machine when you are done.

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)