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:
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.
timeTester() function. You should run
the tests as follows:
>>> import linkqueue, listqueue >>> timeTester(listqueue.Queue(), 100) 0.0002989768981933594 >>> timeTester(linkqueue.Queue(), 100) 0.0005559921264648438For 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.
circle.py will contain a definition for class Circle. Each Circle object is given a position, size, speed, and heading in its constructor. The class should have a draw(self, surface) method that renders the Circle, and a move(self) method that updates its position based on its speed and heading.
triangle.py will contain a definition for class Triangle
. Each Triangle object is given a length, a width, and a position in its constructor. The class should have a draw(self, surface) method that renders the Triangle, and a rotate(self, distance)
method that changes its heading.
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)