Example #1
0
def hotPotato(namelist, numrounds):
    namequeue = Queue()
    for name in namelist:
        namequeue.enqueue(name)

    while namequeue.size() > 1:
        for i in range(numrounds):
            namequeue.enqueue(namequeue.dequeue())
        namequeue.dequeue()

    return namequeue.dequeue()
Example #2
0
def radixSortStrings(listOfStrings):
    ''' Function to sort the items in the listOfStrings using a radixsort algorithm
    '''
    # Create the mainqueue and enqueue the items from the listOfStrings
    mainqueue = Queue()
    for item in listOfStrings:
        mainqueue.enqueue(item)

    # Calculate the max length so that we know how many iterations we need to do
    maxLength = 0
    auxQ = Queue()
    for item in range(len(mainqueue)):
        temp = mainqueue.dequeue() 
        length = len(temp)
        auxQ.enqueue(temp)
        if length > maxLength:
            maxLength = length
    updateQueue(mainqueue,auxQ)

    # Initialize an ordered dictionary auxQueuesDictionary that holds 27 auxiliary queues for 27 characters
    # as keys: ' ' and 26 lower case letters. Make sure the keys are added in
    # alphabetical order so that when we iterate over the dictinary, keys are
    # retrieved in alphbetical order.
    setupAuxQueuesDictionary()
    auxQueueDictionary.__setitem__(" ",0)
    for char in string.ascii_lowercase:
        auxQueueDictionary.__setitem__(char,0)

    #Set up a for loop that sorts the items from the mainqueue based on
    # characters at index maxLength, then the characters at index maxLength-1,
    # maxLength-2, ...index 0. Every time using the auxiliary queues to do the sorting
    
    for strng in mainqueue:
        for index in range(maxLength-1,0,-1):
            char = charAt(strng,index)
            auxQueueDictionary.__setitem__(char,strng)

    for item in auxQueueDictionary
        temp = auxQueueDictionary.pop()
        mainqueue.enqueue(temp)

    sortedList = []
    # mainQueue should be sorted.
    # dequeue the items of the mainqueue into a list and return it
    for item in range(len(mainqueue)):
        temp = mainqueue.dequeue()
        sortedList.append(temp) 
    return sortedList
Example #3
0
class QueueTests(unittest.TestCase):
    def setUp(self):
        self.q = Queue()

    def test_len_returns_0_for_empty_queue(self):
        self.assertEqual(len(self.q), 0)

    def test_len_returns_correct_length_after_enqueue(self):
        self.assertEqual(len(self.q), 0)
        self.q.enqueue(2)
        self.assertEqual(len(self.q), 1)
        self.q.enqueue(4)
        self.assertEqual(len(self.q), 2)
        self.q.enqueue(6)
        self.q.enqueue(8)
        self.q.enqueue(10)
        self.q.enqueue(12)
        self.q.enqueue(14)
        self.q.enqueue(16)
        self.q.enqueue(18)
        self.assertEqual(len(self.q), 9)

    def test_empty_dequeue(self):
        self.assertIsNone(self.q.dequeue())
        self.assertEqual(len(self.q), 0)

    def test_dequeue_respects_order(self):
        self.q.enqueue(100)
        self.q.enqueue(101)
        self.q.enqueue(105)
        self.assertEqual(self.q.dequeue(), 100)
        self.assertEqual(len(self.q), 2)
        self.assertEqual(self.q.dequeue(), 101)
        self.assertEqual(len(self.q), 1)
        self.assertEqual(self.q.dequeue(), 105)
        self.assertEqual(len(self.q), 0)
        self.assertIsNone(self.q.dequeue())
        self.assertEqual(len(self.q), 0)
Example #4
0
def bfs(g, start):
    start.setDistance(0)
    start.setPred(None)
    vertQueue = Queue()
    vertQueue.enqueue(start)
    while vertQueue.size() > 0:
        currentVert = vertQueue.dequeue()
        for nbr in currentVert.getConnections():
            if nbr.getColor() == 'white':
                nbr.setColor('gray')
                nbr.setDistance(currentVert.getDistance() + 1)
                nbr.setPred(currentVert)
                vertQueue.enqueue(nbr)
        currentVert.setColor('black')
Example #5
0
def BFS(maze, v, goal, came_from):
    frontier = Queue()
    frontier.enqueue(v)
    came_from[v] = None
    while not frontier.is_empty():
        v = frontier.dequeue()
        if maze[v[0], v[1]] == Maze.EMPTY:
            if v == goal:
                return v
            else:
                maze[v[0], v[1]] = Maze.OCCUPIED
                for w in maze.getAllMoves(v[0], v[1]):
                    if maze[w[0], w[1]] == Maze.EMPTY:
                        frontier.enqueue(w)
                        came_from[w] = v
    return None
Example #6
0
def bfs(g, start):
    '''
    After running through BFS, all vertices of the graph will have their color, distance and predecessor from the start.
    '''
    start.set_predecessor(None)
    start.set_distance(0)
    vert_queue = Queue()
    vert_queue.enqueue(start)

    while vert_queue.size() != 0:
        cur_vertex = vert_queue.dequeue()

        for nbr in cur_vertex.get_connections():
            if nbr.color == Color.WHITE:
                nbr.set_color(Color.GRAY)
                nbr.set_distance(cur_vertex.get_distance() + 1)
                nbr.set_predecessor(cur_vertex)
                vert_queue.enqueue(nbr)

        cur_vertex.set_color(Color.BLACK)
Example #7
0
class MediaPlayer:
    def __init__(self):
        self.__mediaQueue = Queue()

    def addTrack(self, newTrack):
        self.__mediaQueue.enqueue(newTrack)

    def __str__(self):
        return str(self.__mediaQueue)

    @property        
    def count(self):
        return self.__mediaQueue.size()

    def play(self):
        while self.__mediaQueue.isEmpty() != False:
           currentTrack = self.__mediaQueue.dequeue() 
           print("Now playing",currentTrack) 
           sec = 1
           for s in currentTrack.length:
               time.sleep(1)
               print(sec," of ",currentTrack.length," seconds")
               sec += 1
        else:
            print("There are no tracks in the playlist")        

    def bringToFront(self, fronttrack):
        tracklist =  self.__mediaQueue
        newlist = Queue()
        found = False
        list2 = Queue()
        while not tracklist.isEmpty():
            first = tracklist.peek()
            if first != fronttrack:
                list2.addTrack(first)
                tracklist.dequeue()
            if first = fronttrack:
                found = True
                newlist.enqueue(first)
                tracklist.dequeue    
        else: