Lab 8: Practice with Lists and Dictionaries

Goals

After the lab, you should be proficient at

  1. reading from and writing to files
  2. solving problems with lists
  3. solving more advanced problems using functions, such as passing a list as a parameter to a function
  4. solving problems using dictionaries

Linux

Instead of creating the lab8 directory, copy the directory /home/courses/cs111/handouts/lab8 into your labs directory, using the -r option that you usually use to turnin your code.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab8.1.py through lab8.3.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 lab8.x.out, where x is the problem number.

  1. (10) Create a list that contains the even numbers from 2 to 20, inclusive. Print out the list in two columns that looks like (note the formatting):
     2      12
     4      14
     6      16
     8      18
    10      20
    

    Hint: what are the list positions for each displayed element?

  2. (10) Using a dictionary object, create a program that maps a letter to an example word that starts with that letter. You must have at least three entries in your dictionary. Then, print out the dictionary so that it looks similar to a children's book and the keys are printed in alphabetical order. Example output looks like:
    f is for fiddle
    g is for goose
    z is for zoo
    
  3. (30) The most common last names for people in the United States are Smith, Johnson, and Williams. The most common first names for females are Mary, Patricia, and Linda and for males are James, John, and Robert. (Source: Name Statistics) In this program, you are going to determine the most common names for W & L students.

    This is a larger program. You will have to break it into smaller pieces, solve (and test) a piece, and then move on to the next piece.

    Your program will read in a text file of names (one name per line), count how many times each name occurs in the text file, and write a summary output file containing the names (in alphabetical order) and the number of times each name occurs, in the following format:

    <count> <name>
    

    The format of the output file may be different from what you expected. The reason that it's in this format (count, then name) is so that we can use a Unix utility to view the data in a different way.

    There are three data files in the names_data directory for you to generate summary data files for. Save your output files in the same names_data directory. You should name the summary files appropriately, such as female_fnames_freq.dat. (You can name the files either in your program or using Linux commands.)

    To view the output files you've created, use the commands more or cat or open them up in jedit or idle. Use the Unix commands as
    more <summary_file>

    For each of your summary files, run the command:

    sort -n <summary_file>
    
    and report the top 5 most common names for each category with their frequencies in comments in your program. Any surprises in the results?

    You can also use the grep command to look at the data for a particular name. The syntax of the grep command is grep <searchTerm> <filename>. An example of using this command is

    grep Sprenkle names_data/lname_freq.txt

    If nothing shows up, that name wasn't found in the file.

  4. (50) Deal or No Deal.

    dealornodeal.py contains the main function and some additional functions to play the game Deal or No Deal. For this problem, you will fill in the code for the functions to make the game work correctly. You should not modify the main function, and you should not modify the function headers (i.e., don't modify the number of parameters).

    Note that the given program will not execute successfully.

    First, you should read through the program and get an idea of how it works. For example, how does the program keep track of the cases that have not been opened? How does the program keep track of the amounts that have not been revealed? (Why are these two different variables?) When does the game end?

    Tackle the functions in the following order, writing each one and then testing how far your program gets with each one.

    Testing: You will need to run this program several times to make sure it is working correctly. Test using both of the files in the deal_or_nodeal directory. You can add additional test files.

Extra Credit (up to 10 pts)

Is it possible to make a better offer function? The banker wants to keep the offers low so that the players don't win too much money. However, if the offers are too low, the player will not take the offer and could win more money.

Keep track of how much players are winning/losing in aggregate in a file called payout.dat. We'll consider the payout to be the difference between the player's case and the deal made. If the player does not make a deal, the payout is 0.

Read in the payout from the file. (Start the payout file at 0.) At the end of the game, print the new total payout to the file. Doing this part (reading/writing a file) will earn you 5 extra credit points.

After you've played the game approximately 10 times, you have a pretty good idea of the payouts. (Admittedly, the payout depends on luck and how good your player is.)

Then, try writing a modified offer function and comparing that payout to the other payout. Justify the modifications to the function in comments.

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 lab8 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  3. Use the printLab.sh command to create a file to print out. You should probably print from the labs directory.

    View the lab8.ps file using the gv command, e.g., gv lab8.ps. You should not print out the original data files or the generated summary files.

    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)