Beispiel #1
0
    def test_queue(self):
        q = Queue()
        self.assertEqual(True, q.is_empty())
        self.assertEqual(0, q.size())

        q.enqueue(1)
        self.assertEqual(False, q.is_empty())
        self.assertEqual(1, q.size())

        q.enqueue(True)
        q.enqueue("Hello, World")
        self.assertEqual(3, q.size())

        self.assertEqual(1, q.dequeue())
        self.assertEqual(True, q.dequeue())
        self.assertEqual(1, q.size())

        q.dequeue()
        self.assertEqual(True, q.is_empty())
Beispiel #2
0
    def test_queue(self):
        q = Queue()
        self.assertEqual(True, q.is_empty())
        self.assertEqual(0, q.size())

        q.enqueue(1)
        self.assertEqual(False, q.is_empty())
        self.assertEqual(1, q.size())

        q.enqueue(True)
        q.enqueue("Hello, World")
        self.assertEqual(3, q.size())

        self.assertEqual(1, q.dequeue())
        self.assertEqual(True, q.dequeue())
        self.assertEqual(1, q.size())

        q.dequeue()
        self.assertEqual(True, q.is_empty())
Beispiel #3
0
def find_escape_position(total, count):
    q = Queue()
    for i in xrange(total):
        q.enqueue(i + 1)

    while q.size() > 1:
        for i in xrange(count - 1):
            item = q.dequeue()
            q.enqueue(item)
        q.dequeue()

    return q.dequeue()
Beispiel #4
0
def find_escape_position(total, count):
    q = Queue()
    for i in xrange(total):
        q.enqueue(i+1)

    while q.size() > 1:
        for i in xrange(count-1):
            item = q.dequeue()
            q.enqueue(item)
        q.dequeue()

    return q.dequeue()
Beispiel #5
0
def simulation(numSeconds, pagesPerMinute):

    labprinter = Printer(pagesPerMinute)
    printQueue = Queue()
    waitingtimes = []

    for currentSecond in range(numSeconds):

        if newPrintTask():
            task = Task(currentSecond)
            printQueue.enqueue(task)

        if (not labprinter.busy()) and (not printQueue.is_empty()):
            nexttask = printQueue.dequeue()
            waitingtimes.append(nexttask.waitTime(currentSecond))
            labprinter.startNext(nexttask)

        labprinter.tick()

    averageWait = sum(waitingtimes) / len(waitingtimes)
    print("Finished tasks %3d  Average Wait %6.2f secs %3d tasks remaining." %
          (len(waitingtimes), averageWait, printQueue.size()))