CSCI 150 - Foundations of Computer Science

Spring 2012

Programming Assignment 11: Creating new classes

Reading Assignment

Queues

When a person waits in line, everyone in the line prior to that person has priority for exiting the line. A data structure that simulates this behavior is called a queue. Here is one possible implementation of a queue:
class Queue(object):
    def __init__(self):
        self.q = []

    def add(self, item):
        self.q.append(item)

    def remove(self):
        return self.q.pop(0)

    def isEmpty(self):
        return len(self.q) == 0
This implementation suffers from an efficiency problem. When removing the first element of the list, all the other elements must be moved backwards by one location. For this assignment, you will implement a more efficient queue and compare the running times. Download the following files: The program linkqueue.py uses a data structure called a linked list to store the queue elements. Each element has a reference to the element behind it in line, and the queue as a whole tracks the first and last elements. New elements are added to the end of the line; the former last element is given a reference to the new last element. When removing an element, the new first element is determined by examining that reference. Your task is to implement the remove() and __contains()__ methods.

Timing

Once your implementation is functional, you will need to compare the timings of the two queues using the timeTester() function. You should run the tests as follows:
>>> import linkqueue, listqueue
>>> timeTester(listqueue.Queue(), 100)
0.0002989768981933594
>>> timeTester(linkqueue.Queue(), 100)
0.0005559921264648438
For small numbers of items, the performance will be similar. It is for large numbers that the performance diverges. For each value of n from 10,000 to 200,000, in increments of 10,000, record the time taken by each type of queue. Store your data in a text file called queueData.txt, and submit it along with your completed linkqueue.py.

Animation

You will use Pygame to write programs that move shapes around a window. (Pygame is installed on all of the lab computers in MCReynolds 314.)

Example pygame animations

Programming Assignment

You will write two programs:

Test Code

To test circle.py, copy the clocked() function (getting rid of the vector parameter) from moving_square.py into circle.py. Add the following function definition to circle.py, and just type test() at the command line to test it:

def test():
    shape = Circle((320, 240), 50, 0.9, math.pi/4)
    size = (640, 480)
    clocked(shape, size, 50)

To test triangle.py, copy the leftRight() function from dash.py into triangle.py. Add the following function definition to triangle.py, and just type test() at the command line to test it:

def test():
    size = (640, 480)
    shape = Triangle(40, 20, (320,240))
    leftRight(shape, size, math.pi / 12)