CS 151: Programming Assignment 1

Introduction to Python and Search [60 points]
due Wednesday, January 26, 11:55pm

Please read and follow the submission instructions on the assignment page

This is a SOLO assignment.  Everyone must do their own work on this assignment (but you may verbally discuss the assignment with others if you want).

In this course you will complete your programming assignments in Python. I chose Python over other textbook-supported languages (LISP and Java) because (1) Python is easy to use and fairly easy to learn, (2) one of the more complete libraries associated with the textbook is in Python, and (3) Python is a highly useful language to know.

The purpose of this assignment is to help you get comfortable programming in Python.  This assignment will NOT teach you everything you need to know about Python; thus, another purpose of this assignment is to help familiarize you with the Python documentation.  Keep this documentation handy throughout the semester.

Part 1: Getting Started

Python is installed on all of the lab machines in Beckman B102 and B105, but I recommend that you also install it on your own computer.  You can obtain Python at www.python.org/download/.  

After installing Python you can immediately run it by choosing "Python (command line)" from the Python menu from the programs part of your start menu.  You should see Python start in a command window.  Alternatively you can start python by opening a command window and typing "python" at the prompt.  Of course you need to add Python to your path if you are not running from within Python's directory.  

Python is an interpreted language, which means you can type commands directly at the prompt.  Try the following, pressing enter after each one.  Notice that you do NOT need semi-colons at the end of each line, nor do you need to declare variables before you use them (Python has implicit typing, like Rex).

>> 10**4

>> a = 'ai is cool'

>> len(a)

>> a.split()

>> myL = [1, 2, 3, 4]

>> myL[1:3]

>> myL[1:]

Can you understand what each of the above lines of code does?  You'll get more practice with lists and strings later, but you may also want to check out the documentation.

Of course, you will usually want to save the programs you write.  Like in almost any other language, you can write programs in a file and then load them into the Python interpreter.  Python comes with its own editor, called IDLE.  In my opinion IDLE is pretty good, but you may use any editor you like.  jEdit, Crimson and Emacs are other reasonable options.

Open your editor of choice and type in (or copy) the following code that defines a simple list search function:

def listSearch( L, x ):
    """ Searches through a list L for the element x"""
    for item in L:
        if item == x:
            return True    # We found it, so return True
    return False           # Item not found    

Save your file as "search.py".

Notice a few things about the syntax of this function:
To run this function, you first need to load the code into the interpreter.   From IDLE, simply press F5 to load the function.  You should see a Python prompt (but it should look like nothing really happened).  If you are not using IDLE, in a command prompt navigate to the directory containing the file you just created and run Python (by typing "python").  Then type:
>> import search ; reload(search) ; from search import *
This line is overkill for loading your file, but it will definitely load and/or reload everything you have written in the file search.py so I suggest copying and pasting this line for any file you are trying to load into Python in the future (changing the filename as appropriate, of course).

Now that the program is loaded you can run any functions defined in the file (in this case just one).  Try it out, e.g.:

>> listSearch([2, 5, 1, 6, 10], 1])
True

Part 2: Programming in Python

You're now set up and ready to go.  Complete each of the following problems.  Be sure to include docstrings for every function you write.

  1. Objects and Games [20 points]. Complete this problem in a file called pa1pr1.py
    In this problem you will develop an object for managing a game of Tic Tac Toe.  While we covered the basics of creating objects in Python in class, you will probably want to refer to Python's documentation on creating classes.  Create a class called TTTBoard that defines the following functions:
    You may represent the board however you like.  You may define other functions as well.  You may wish also to define a function that allows two players to play the game to make sure your above functions are working correctly.

  2. Missionaries and Cannibals [40 points] (Based on AIMA Ex 3.9) Complete this problem is a file called pa1pr2.py.
    In this problem you will formulate and solve the famous "Missionaries and Cannibals" problem.  (You'll remembe a similar homework from CS60, but this time we won't use Prolog, so the search is explicit).  The problem goes like this:

    There are 3 missionaries and 3 cannibals on one side of a river.  They have 1 boat, that can hold 1 or 2 people.  The goal is to get everyone across the river without ever leaving more cannibals than missionaries alone on one side at a time (if there are 0 missionaries, that's OK).  

    This problem is famous in AI because it was used in the first paper that presented problem formulation from an analytical viewpoint (AIMA, 115).

    a. Formulate this problem precisely as a search problem. First write a class named MissionariesAndCannibals that defines the state space.  This class must include (among any other functions you wish to define):
       
    b. Now write a class named SearchNode that keeps track of the information needed to search for the solution.  This class should be flexible, and should not be tied to a specific search algorithm.  It should include the following:

    c. Finally, outside of your two classes, write a function named SearchForSolution() that starts at the initial state for the Missionaries and Cannibals problem, and searches until it reaches the goal state.  Your function should return the SearchNode object that contains the first goal state that you reach in your search.  You may implement any search algorithm you like.  In a comment above your function, state which algorithm you chose and why.
       Hint: If you are using DFS, you'll need to keep track of states you've already seen to avoid going into an infinite loop.
When you are done, submit your solutions to both problems though the Sakai site.