# 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:
• Properly documented code
• Well-chosen variable names
• Modular code
• Code reuse when appropriate

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:
• The user should be able to invoke it from the command line as: textGetter.py (someURL)
• The 'main' function should then call a function called urlGetter that takes the URL as input. This function should return a string containing the entire contents on that URL. Your function should be smart enough to prepend "http://" to the URL if it's not there already.(hint: if you're using more than four lines, there's an easier way ... try looking at the urllib module.)
• 'main' should then call a separate function called 'tagStripper' that takes as input the contents of a web page and returns the same page with all HTML tags stripped out. For purposes of this assignment, you may assume that anything between < and > is a tag.
There are (at least) three ways to do this - any are acceptable. Try them all to get some familiarity with the different ways of doing things in Python.
• By hand
• Using the re (regular expression) module
• Using the HTMLparser module.
• Finally, your program should print out all non-tag words in the document, one word to a line.