Homework Assignment 9

For this assignment there are several requirements:

  1. You must use Python to write all the programs in this assignment.
  2. You do not have to do any error checking. If an exception occurs, your program does not have to handle it. Instead just let your program terminate.
  3. You must use {}-style formatting rather than %-style formatting for printing your strings. See my Python IO notes for a discussion of how to use {}-style formatting. The %-style is considered old-style and you should not get into the habit of using it.

  1. Write a program named tail.py that prints the last n lines of a file. The program should take two command line arguments which are the name of the file and the number of lines to print. Remember that you can use minus subscripts and array slices to simplify your task.

  2. Write a program named frequency.py that prints each word that occurs in a file and the number of times that word occurs. Your program should take a single command line argument, which is the name of the file, and it should print the words by increasing frequency, using the word itself as a secondary sort key. Each word should be printed in a left-justified field of 20 characters and each word count should be printed in a right-justified field of 5 characters. For example, if the contents of the file is:
    The quick brown fox jumped
    over the fence and then
    the fox jumped back over
    the fence.
    then your output should be:
    The                      1
    and                      1
    back                     1
    brown                    1
    fence                    1
    fence.                   1
    quick                    1
    then                     1
    fox                      2
    jumped                   2
    over                     2
    the                      3
    Note that uppercase is significant (The and the are different) and so is punctuation (fence and fence. are different).

  3. Write a python program named tree.py that creates a class called BinaryTree that supports the following API:

    You should use an unbalanced binary search tree to implement your class. Your program may define additional helper methods or helper functions, and it may also define additional classes (e.g., you may wish to declare a TreeNode class).

    I have provided a driver program named BinaryTreeDriver.py that you can use to test your program. It takes one command line argument, which is either "-", which will invoke a command line interpreter, or a filename, in which case the driver will read and execute commands from the file. The driver accepts the following three commands:

    This program will test your binary tree class on strings. However, you should be sure that it works with other types of objects as well (e.g., numbers, a class, etc)

  4. Implement problem 2 from homework 2--the golf handicap problem--using Python rather than Java. Name your main file handicap.py and put any class definitions in a file named golfer.py. These are the only two files you should use. For kicks you should type "wc" on both your python and java implementations to see how many lines they take. My Python implementation took roughly 100 lines and my Java implementation took roughly 250 lines, for a 60% reduction in code.

    Here are a couple hints:

    1. You can use array slices to access just part of a list. For example, when you need the first 20 entries from a list, you can access them using the notation list[0:20].
    2. You can use a string's join command to quickly piece together a course name without having to write a for loop that concatenates the pieces of the course name together.
    3. You can use Python's datetime module to conveniently handle dates (you can google for its documentation).

What to Submit

Submit the following files:

  1. tail.py
  2. frequency.py
  3. tree.py
  4. handicap.py, golfer.py