Skip to main content.

Lab 6: ASCII Representation, Lists, and Files

Goals

After the lab, you should be proficient at

  1. using the ASCII representations of characters with built-in functions
  2. solving problems with lists
  3. reading and processing data from files

Objective: Review

Review the slides for today.

Objective: Linux

Copy /csdept/courses/cs111/handouts/lab6 and all of its contents, recursively, into your cs111 directory. This way, you don't have to create the lab6 directory first.

Otherwise, you could first create the lab6 directory and then copy, recursively, all of the contents of the /csdept/courses/cs111/handouts/lab6 directory (i.e., add a * to the source) into your lab6 directory.

Expected result: You will have a directory called lab6 that contains a directory named data that contains files that we will use as input in this lab.

Objective: Help Client Set Up

Run runHelpClient &

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.

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

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

    The problems below have multiple steps. Read all of the instructions and confirm that you have met all requirements before submission.

  4. (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. Recall that constants are used when we have values that don't change and when we would prefer to use words (i.e., variable names) to give meaning to numbers, which we humans do not comprehend as readily.

    Example output:

    The randomly generated password is eNu7LFaW
    
    The randomly generated password is uEW7j9s
    
    The randomly generated password is 7KslNy
    
  5. (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.

  6. (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 the handouts directory.

    Hardcode the filename to start. Then, add the user input after you have made sure that the program works.

    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? Make a file for testing in your data directory. 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?

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

    1. IDLE and jEdit may create backup files with the "~" extension. Delete these files from your lab directory to save paper when you print.
    2. Copy your lab6 directory into your turnin directory.

    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)