Homework 1: Introduction to Python

Due: Tuesday, September 6 at the start of class.

What to turn in: Please create a directory called 'homework1' in your submit directory and place all your source code in there before the beginning of class. Also, please bring a printout of your source code, with your name on it, to class.

This assignment is meant to ``encourage'' you to get a basic familiarity with Python. Python is one of the languages we'll be using in the course; all of the code accompanying the textbook is in Python.

You should use the references on the course homepage to help you learn Python; if you're familiar with a high-level language such as Java or Perl, much of Python will look pretty familiar.

You will be expected to use good software engineering principles in this assignment (as in all assignments). This includes:
You will be graded according to both style and functionality.

Also, please read the assignments carefully before starting and make sure that you a) do the tasks that are requested and b) complete all of the requirements of the assignment.

Assignment: For each of the following problems, write a Python function to solve the problem. You should also provide a 'main' function that allows your code to be run from the command line.
  1. Fibonacci numbers. The Fibonacci sequence is defined as:
    f(0) = 0
    f(1) = 1
    f(n) = f(n-1) + f(n-2) for n > 1

    The first few numbers are: 0 1 1 2 3 5 8 13 21 34 ...

    1. Write a version of Fibonacci that takes an integer n as input and uses recursion to compute and print out each of the first n Fibonacci numbers. You may repeatedly call the recusive function within a loop if you like, but you should use recursion to compute each of the numbers.
    2. Write a version of Fibonacci that uses iteration to compute each of the first n Fibonacci numbers.

  2. word count. Write a version of the unix command wc. It should take a filename as input on the command line and produce as output a list of the number of characters, words and lines in the file. For example:
    [brooks@valis assignments]$ wc.py homework1.html
      84  510 2964 homework1.html
    (this should not take more than 10 lines)

  3. This problem is a bit larger. What you will do for this is to write a program called textGetter.py. It should do the following: