Lab 6: Lists, Files, and Functions

Goals

After the lab, you should be proficient at

  1. solving problems with lists
  2. reading and processing data from files, including numeric data
  3. writing data to files
  4. using functions within your program

Linux

Copy /home/courses/cs111/handouts/lab6 and all of its contents, recursively, into your labs directory. This way, you don't have to create the lab6 directory first. Otherwise, you could first create the lab6 directory and then copy all of the contents of the /home/courses/cs111/handouts/lab6 directory (i.e., add a * to the cp command) into your lab6 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 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]. Reference the Fibonacci sequence example programs from class.

    Print out 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. (Hint: use one of the string methods to break the phrase into a list of words and another method to make the resulting phrase all lowercase.) 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
    
  3. (15) Write a program that encodes many phrases using the Caesar Cipher program from last lab. If your program didn't work, or you want to see mine, see caesar.py. 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.

    Example Output:

    This program will encode a whole file using Caesar ciphers.
    
    Enter the name of a file to encode: 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
    

    Doesn't that make your testing a little easier, since you don't need to type out the whole phrase every time you test?

  4. (15) Copy the previous program and modify it so that it writes the encoded message to a file. The program should take as input the encoded message output file's name. You may want to assume that the output files always get written to the data directory so that you don't clutter your lab6 directory with files.

    Note: I broke up a larger problem (extending the Caesar cipher program) into multiple, smaller problems, each of which is easier to tackle than tackling the whole problem all at once. This is an important problem-solving skill to develop. You may feel like breaking the problem into smaller problems slows you down, but it usually doesn't because you spend less time debugging the smaller pieces than if you had tackled it all at once. You'll get lots of practice solving problems in this class--including on the next problem!

  5. (30) The system of scoring an Olympic gymnast is based on two separate panels of scores. The A panel judges the requirements, difficulty, and connections of a routine. The scoring starts at zero and then adds points accordingly. The B panel judges the execution of a routine, and the scoring starts at 10 with points deducted accordingly for execution and for any applicable violations such as stepping out of bounds or being over the time limit.

    For each panel of judges, the lowest and highest scores are dropped (to prevent judges from biasing the results) and then averaged. The two panels are then added together for the final score. A very good score will range in the 15s and 16s.

    Your task: Read in a file containing the gymnastics scores. (The file name can be a constant in your program.) The first line of the file is the average difficulty score. The remaining six lines are the judges' execution scores. (You cannot assume that the execution scores are sorted.) Calculate the average score, as described above. Display the judges' scores and the final score.

    Example input file:

    5.7
    8.3
    9.1
    8.0
    8.9
    8.8
    8.5
    

    Example output:

    Gymnastics Scores for data/scores.dat
    --------------------------------------------------------
            Judges Execution Scores: 8.0 8.3 8.5 8.8 8.9 9.1 
            Average Execution Score: 8.625
            Average Difficulty Score: 5.7
            The Final Score: 14.325
    

    Your output does not need to display the scores in sorted order.

    Example files are data/nastia.dat and data/shawn.dat

    Break this problem into pieces (what are those pieces?) and tackle them one at a time.

  6. (15) Open lab6.6.py, which you copied at the beginning of lab. Modify the program so that it implements what is described in the comments, e.g., draws and moves bugs, using functions defined within the program.

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 the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  3. Clean up: jEdit makes backup files and appends "~" to the name of your file. Delete any "~" files from your lab directory.
  4. Before printing, move the graphics.py and graphics.pyc (actually, the latter can be deleted rather than moved) files out of your lab6 directory; otherwise, the print out will be long and screwed up. You can move those files back into your lab6 directory after you've printed. In other words, you should only have the .py files you wrote and the .out files you created in your directory when you print.
  5. Turn in your printed lab assignment, using the 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!

Grading (100 pts)