Homework 1

From 118wiki

Jump to: navigation, search


Homework 1

This is an introductory Python homework.


This homework is due by Midnight (11:59pm) on Thursday September 14, 2006.

Submit your solution using the submit command on one of the department's Linux machines to the course directory. (Note: if you don't already have an account on the departmental machines, I'll grant you an account for this course; please contact me to get your account.) Here are instructions on how to submit:

  1. In your directory, rename, move, or delete any directory named homework1
  2. Download media:Homework1.zip, and then execute "unzip Homework1.zip". This should create a directory called homework1. You will modify files in that directory to complete your homework.
  3. When your work is ready, run the submit script from your home directory by typing submit at the command prompt and hitting return/enter.
  4. When prompted for the File/directory name, enter homework1
  5. When prompted for the course put c118
  6. Next you will see a list of possible submission directories. For this assignment, reply homework1
  7. Done! For further help, please review the submit proceedure provided by CSG: submit.html
  8. Note: case does matter.
  9. Note: submitting the same homework assignment multiple times will overwrite the previously submitted homework with the same name and submit location.

Background on Unit Testing

Python designers have provided several facilities for unit testing of modules and functions. We'll use a simple form, which is illustrated by the following example.

Module to reverse a string by words.  Following are test cases.
>>> reverseWords('four score and seven years') 
'years seven and score four'
>>> reverseWords('one') 
def reverseWords(astring): 
 words = astring.split() 
 return ' '.join(words) 
if __name__ == '__main__': 
 import doctest, sys 

Notice that the "docstring" comment for this module includes the transcript of an interactive Python session. There are two ways this module could be used: first, it could be used by another Python program that has an "import reverse" or a "from reverse import *" statement. Second, this module could be executed directly from the command line by "python reverse.py" or by "python -i reverse.py". In these second cases, Python will execute the "if" statement comparing '__name__' to the string '__main__' and this will be True because the reverse program is being executed from the command line directly. Therefore, Python will use the doctest module to try test cases -- which are exactly the ones in the docstring -- for the reverseWords function.

If all goes well, meaning that the program has no bugs, then the program executes without any response (meaning that every test case passed without error). If you tried this, you would see no unusual response. However, if you changed the docstring to have

>>> reverseWords('one') 

and then execute the program, you would see an error telling the the expected output ('two') was not the same as what reverseWords('one') produced.

Unit Testing and Homework

The solution to your homework will be simple to test using Python's unit testing facility. First, you download a "skeleton" of the homework, which contains a docstring with test cases, and some partially written program. Download from here: media:Homework1.zip and unzip the homework1 directory. When you look at the files in homework1, you'll see that each has a missing function. That's your job, to write the missing functions.

There are four files in the directory, revthird.py, treedepth.py, vectest.py, and wordfreq.py. You'll know your assignment is done when "python treedepth.py" (and similarly for the other three) completes with no errors.


  • Each of the functions you need to write can be done in no more than 5-10 lines of Python code, and no unusual library calls are needed for your solutions.
  • In grading the solutions, we will add extra test cases to ensure that your solutions aren't too specific to the test cases in the docstrings.

Homework 1 Solutions

revthird function

def revthird(x):
  for i in range(0,1+len(x),3):
    if i>0:
      t = list(x[i-1])
  return x

wordfreq function

def wordfreq(ref,input):
  h = input.split(' ')
  f = []
  for x in ref: f += [ h.count(x) ]
  return f

vectest function

def dotprod(x,y):
   s = 0
   for i in range(len(x)): s += x[i]*y[i]
   return s
def vectest(x):
   for i in range(len(x)):
     for j in range(len(x)):
        if i<j:
           if dotprod(x[i],x[j]) == 0: return False
   return True

treedepth function

def treedepth(x):
  if type(x) != type((0,)):  return 0
  y = list(x)
  d = 0
  for m in y:
     r = treedepth(m)
     if r>d:  d = r
  return 1+d
Personal tools