CS 662 Assignment 1: Basic Python

Assigned: Tuesday, August 29.
Due: Tuesday, September 5 at the start of class.


What to turn in: Please create a directory called 'assignment1' 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 help you to get a basic familiarity with 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. To get full credit, your code must perform all functionality as specified, and use good SE principles.

Also, please read the problems 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.

Problem 1: ROT-13 encrypter/decrypter. (10 points)

ROT-13 is a very simple encryption scheme that's sometimes used to mask ASCII text. The algorithm simply shifts all characters forward a fixed offset (modulo 26) to encode them, and backwards to decode them. For example, with an offset of 5, "Hello" becomes "Mjqqt". For our purposes, non-alphabetic characters, such as space and comma, can be ignored.

For this problem, you should write a python program which can do ROT-13 encoding and decoding. Your program should allow the user to specify whether the input string is to be encoded or decoded, and optionally specify an offset. (Use 13 as the default if no offset is provided). It should run from the command line and allow the user to provide all parameters as command line arguments. It should also handle errors gracefully.

Hints: Sample usage:
 brooks$ python rot13.py -e 'Hello World'
Uryyb Jbeyq
 brooks$ python rot13.py -d 'Uryyb Jbeyq'
Hello World
 brooks$ python rot13.py -e -o 7 'Hello World'
Olssv Dvysk
 brooks$ python rot13.py -o 7 -d 'Olssv Dvysk'
Hello World
 brooks$ python rot13.py -o 7 -d -q 'Olssv Dvysk'
Usage: rot13.py {-d|-e} (-o n) string
Problem 2: Tag stripper (10 points)

Write a python program that can take as input a URL, fetch that web page, and strip out all of the HTML tags. (For purposes of this assignment, anything between < and > is a tag) You should implement two methods for doing this: One in which you scan the document 'by hand' and keep track of opening and closing brackets, and one in which you use the re module to replace tags with empty strings. Your program should also have a command-line argument that allows the user to specify which method is used. (Have by hand be the default)

Hints: Sample usage:
brooks$ python tagstripper.py -r http://www.google.com
Google



Personalized Home | Sign in
Web    Images    VideoNew!    News    Maps    more »
BooksFroogleGroupseven more »   Advanced Search  Preferences  Language ToolsAdvertising Programs - Business Solutions - About Google©2006 Google

brooks$ python tagstripper.py http://www.google.com
Google



Personalized Home | Sign in
Web    Images    VideoNew!    News    Maps    more »
BooksFroogleGroupseven more »   Advanced Search  Preferences  Language ToolsAdvertising Programs - Business Solutions - About Google©2006 Google

brooks$ python tagstripper.py -q http://www.google.com
Usage: tagstripper.py {-r|-h} URL
Problem 3: Word counter (10 points)

Write a Python program that can take a string as input from the command line and create a dictionary that maps each word in the string to the number of times it occurs in the string. Within the program, you should have a method that creates the dictionary, and then nicely display the dictionary in your main method. You should be able to combine this with the program from problem 2 to count the words in a web page.

Some hints: Sample Usage:
brooks$ python wordcounter.py 'cat cat dog Cat dog'
Cat : 1
dog : 2
cat : 2
brooks$ python tagStripper.py http://www.cs.usfca.edu | python ./wordcounter.py 
all : 1
usfca : 1
particularly : 1
:) : 1
office : 1
demand : 1
developed : 1
Night : 2

(etc)