def main(): fp = open(input("Insert Filename (with extension) for input: ").strip()) output = sys.stdout # comment back in for writing output to the file, mandatory for grading purposes. # output = open(input("Insert Filename (with extension) for output: ").strip(), 'w') q = CircularQueue() for command in fp.readlines(): q.parse_command(command.strip('\n')) print(q, file=output) fp.close()
def test_accessors(self): queue = CircularQueue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) queue.enqueue(4) assert not queue.is_empty() assert len(queue) == 4 assert queue.get_total() == 10 assert queue.tail_element() == 4 assert queue.head_element() == 1
def test_grow(self): queue = CircularQueue(1) for i in range(0, 16): queue.enqueue(i) assert queue.data == [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None ] assert queue.capacity == 32 assert queue.size == 16 assert queue.head == 0 assert queue.tail == 16
class CircularQueueTest(object): def __init__(self): self.queue = CircularQueue() def _enqueue_dequeue_test(self): """ enqueue和dequeue方法测试 :return: None """ print("enqueue和dequeue方法测试中....") print("循环队列:", self.queue) size = 10 print("开始enqueue元素%d次" % size) for i in range(size): self.queue.enqueue(i) print("当前第3个元素为", self.queue[2]) print("enqueue结束,当前循环队列为:", self.queue) print("当前循环队列长度为:", len(self.queue)) print("顶部元素为:", self.queue.first()) print("开始dequeue元素%d次" % size) for i in range(size): self.queue.dequeue() print("dequeue结束,当前循环队列为:", self.queue) print("当前队列应该为空:", self.queue.is_empty()) def _rotate_test(self): """ rotate方法测试 :return: None """ print("\n" + "rotate方法测试中....") print("添加元素") for i in range(10): self.queue.enqueue(i) print("当前循环列表:", self.queue) print("执行rotate方法") self.queue.rotate() print("当前循环列表:", self.queue) def run(self): """ 执行测试方法 :return: None """ self._enqueue_dequeue_test() self._rotate_test()
def main(): circular = CircularQueue(100000) bounded = BoundedQueue(100000) for i in range(100000): circular.enqueue(i) bounded.enqueue(i) start1 = time.time() while not circular.isEmpty(): circular.dequeue() end1 = time.time() circulartime = end1 - start1 start2 = time.time() while not bounded.isEmpty(): bounded.dequeue() end2 = time.time() boundedtime = end2 - start2 print('For Bounded Queue, the total runtime of dequeing 100000 is:\n' + str(boundedtime)) print('For Circular Queue, the total runtime of dequeing 100000 is:\n' + str(circulartime))
def main(): # create queues regular_lineup = CircularQueue(3) vip_lineup = CircularQueue(3) action = "" while action.lower() != "exit": # get action action = input("Add, Serve, or Exit: ") # add customer to queue if action.lower() == "add": add(vip_lineup, regular_lineup) elif action.lower() == "serve": serve(vip_lineup, regular_lineup) print("people in the line: " + str(regular_lineup)) print("VIP customers queue: " + str(vip_lineup) + "\n") print("Quitting")
def main(): # create bounded and circular queues bounded = BoundedQueue(1000000001) circular = CircularQueue(1000000001) # add items to both queues for i in range(5000000): bounded.enqueue(i) circular.enqueue(i) # dequeue Bounded and analyse time n = 990 totalTimeBounded = 0.0 for i in range(10): start = time.time() for i in range(500): bounded.dequeue() end = time.time() print("Bounded queue required %10.7f seconds" % (end - start)) totalTimeBounded = totalTimeBounded + end - start print("the average time for Bounded queue was %10.7f for n=%d" % (totalTimeBounded / 10, n)) # dequeue Circular and analyse time n = 990 totalTimeCircular = 0.0 for i in range(10): start = time.time() for i in range(50000): circular.dequeue() end = time.time() print("Circular queue required %10.7f seconds" % (end - start)) totalTimeCircular = totalTimeCircular + end - start print("the average time for Circular queue was %10.7f for n=%d" % (totalTimeCircular / 10, n))
def test_6(): """ Test for sequence of enqueues and dequeues and get average :return: None """ queue = CircularQueue() rand.seed(10) # test average every 25 # dequeue every 3 nodes for i in range(1, 101): x = rand.randint(-1000, 1000) queue.enqueue(x) queue.enqueue(x) if i % 25 == 0: print(f"Average: {queue.get_average()}\n") if i % 3 == 0: queue.dequeue() print(queue) print(f"Average: {queue.get_average()}\n")
def sample_test(): """ test for empty queue and simple enqueues :return: None """ queue = CircularQueue() print(queue) print(f"Average: {queue.get_average()}\n") queue.enqueue(23) queue.enqueue(42) queue.enqueue(2) queue.enqueue(195) print(queue) print(f"Average: {queue.get_average()}\n")
def test_grow(self): # mimir test queue = CircularQueue(5) for i in range(0, 8): queue.enqueue(i * 2) assert queue.data == [0, 2, 4, 6, 8, 10, 12, 14, None, None] assert queue.capacity == 10 assert queue.size == 8 assert queue.head == 0 assert queue.tail == 8 # small grow queue = CircularQueue(5) for i in range(0, 8): queue.enqueue(i * 2) assert queue.data == [0, 2, 4, 6, 8, 10, 12, 14, None, None] assert queue.capacity == 10 assert queue.size == 8 assert queue.head == 0 assert queue.tail == 8
def test_enqueue(self): queue = CircularQueue() queue.enqueue(10) queue.enqueue(20) queue.enqueue(30) assert queue.data == [10, 20, 30, None] assert queue.size == 3 assert queue.head == 0 assert queue.tail == 3 assert queue.capacity == 4
def main(): b = BoundedQueue(10000) c = CircularQueue(10000) for i in range(10000): b.enqueue(i) c.enqueue(i) start1 = time.time() for i in range(10000): b.dequeue() print("BoundedQueue running time is:", time.time() - start1) start2 = time.time() for i in range(10000): c.dequeue() print("CircularQueue running time is:", time.time() - start2)
def test_dequeue(self): queue = CircularQueue(6) for i in range(0, 5): queue.enqueue(i * 5) assert queue.data == [0, 5, 10, 15, 20, None] assert queue.size == 5 assert queue.capacity == 6 assert queue.head == 0 assert queue.tail == 5 queue.dequeue() assert queue.data == [None, 5, 10, 15, 20, None] assert queue.size == 4 assert queue.capacity == 6 assert queue.head == 1 assert queue.tail == 5
def test_2(): """ test for valid resizing, should only call resize 1 time :return: None """ queue = CircularQueue(5) for el in range(2, 12, 2): queue.enqueue(el) print(queue) print(f"Average: {queue.get_average()}\n") # should invoke resize only once for el in range(12, 22, 2): queue.enqueue(el) print(queue) print(f"Average: {queue.get_average()}\n")
def test_shrink(self): queue = CircularQueue() for i in range(0, 5): queue.enqueue(i) assert queue.data == [0, 1, 2, 3, 4, None, None, None] assert queue.size == 5 assert queue.capacity == 8 assert queue.head == 0 assert queue.tail == 5 for _ in range(3): queue.dequeue() print(queue.data) assert queue.data == [3, 4, None, None] assert queue.size == 2 assert queue.capacity == 4 assert queue.head == 0 assert queue.tail == 2
def main(): normal = CircularQueue(3) vip = CircularQueue(3) Loop = True while Loop: choose = input("Add, Serve, or Exit: ") if choose == "add": names = input("Enter the name of the person to add: ") types = input("Is the customer VIP? ") if types == "True": if not vip.isFull(): vip.enqueue(names) print("add", names, "to VIP line.") else: print("Error: vip customer queue is full") elif types == "False": if not normal.isFull(): normal.enqueue(names) print("add", names, "to the line.") else: print("Error: Normal customer queue is full") else: print("invild name input") print("people in the line:", normal) print("VIP customers queue:", vip) Loop = True elif choose == "serve": if not vip.isEmpty(): print(vip.dequeue(), "has been served") else: if not normal.isEmpty(): normal.dequeue() else: print("Error: Queues are empty") print("people in the line:", normal) print("VIP customers queue:", vip) elif choose == "exit": print("Quitting") break else: Loop = True print("Invild input")
def main(): test = CircularQueue(15) test.enqueue(3) test.enqueue(3) test.enqueue(3) test.enqueue(3) test.enqueue(3) test.enqueue(3) test.enqueue(3) test.enqueue(3) print(test) print('REMOVE') test.dequeue() test.dequeue() test.enqueue(5) test.enqueue(5) test.enqueue(5) test.enqueue(5) test.dequeue() test.dequeue() test.dequeue() test.dequeue() test.dequeue() # test.dequeue() # test.dequeue() # test.dequeue() # test.dequeue() # test.dequeue() print(test)
def test_dequeue(self): # mimir test copied queue = CircularQueue(6) for i in range(0, 5): queue.enqueue(i * 5) assert queue.data == [0, 5, 10, 15, 20, None] assert queue.size == 5 assert queue.capacity == 6 assert queue.head == 0 assert queue.tail == 5 queue.dequeue() assert queue.data == [None, 5, 10, 15, 20, None] assert queue.size == 4 assert queue.capacity == 6 assert queue.head == 1 assert queue.tail == 5 # smaller test queue = CircularQueue(4) for i in range(0, 3): queue.enqueue(i * 5) assert queue.data == [0, 5, 10, None] assert queue.size == 3 assert queue.capacity == 4 assert queue.head == 0 assert queue.tail == 3 queue.dequeue() assert queue.data == [None, 5, 10, None] assert queue.size == 2 assert queue.capacity == 4 assert queue.head == 1 assert queue.tail == 3 # make sure capacity is never < 4 queue = CircularQueue(6) queue.enqueue(5) assert queue.data == [5, None, None, None, None, None] assert queue.size == 1 assert queue.capacity == 6 assert queue.head == 0 assert queue.tail == 1 queue.dequeue() assert queue.data == [None, None, None, None, None, None] assert queue.size == 0 assert queue.capacity == 6 assert queue.head == 0 assert queue.tail == 0 # normal shrinkage queue = CircularQueue(10) queue.enqueue(5) queue.enqueue(6) assert queue.data == [5, 6, None, None, None, None, None, None, None, None] assert queue.size == 2 assert queue.capacity == 10 assert queue.head == 0 assert queue.tail == 2 queue.dequeue() assert queue.data == [6, None, None, None, None] assert queue.size == 1 assert queue.capacity == 5 assert queue.head == 0 assert queue.tail == 1 queue.dequeue() assert queue.data == [None, None, None, None, None] assert queue.size == 0 assert queue.capacity == 5 assert queue.head == 0 assert queue.tail == 0 # make sure it doesn't shrink queue = CircularQueue(10) for i in range(1, 6): queue.enqueue(i) assert queue.data == [1, 2, 3, 4, 5, None, None, None, None, None] assert queue.size == 5 assert queue.capacity == 10 assert queue.head == 0 assert queue.tail == 5 queue.dequeue() queue.dequeue() assert queue.data == [None, None, 3, 4, 5, None, None, None, None, None] assert queue.size == 3 assert queue.capacity == 10 assert queue.head == 2 assert queue.tail == 5
def test_shrink(self): queue = CircularQueue() for i in range(0, 5): queue.enqueue(i) assert queue.data == [0, 1, 2, 3, 4, None, None, None] assert queue.size == 5 assert queue.capacity == 8 assert queue.head == 0 assert queue.tail == 5 for _ in range(3): queue.dequeue() assert queue.data == [3, 4, None, None] assert queue.size == 2 assert queue.capacity == 4 assert queue.head == 0 assert queue.tail == 2 # empty list that shouldnt shrink queue = CircularQueue(7) assert queue.data == [None, None, None, None, None, None, None] assert queue.size == 0 assert queue.capacity == 7 assert queue.head == 0 assert queue.tail == 0 queue.dequeue() test = queue.dequeue() assert test == None assert queue.data == [None, None, None, None, None, None, None] assert queue.size == 0 assert queue.capacity == 7 assert queue.head == 0 assert queue.tail == 0 assert queue.total == 0 # empty list that has the ability to shrink but shouldn't queue = CircularQueue(8) assert queue.data == [None, None, None, None, None, None, None, None] assert queue.size == 0 assert queue.capacity == 8 assert queue.head == 0 assert queue.tail == 0 test = queue.dequeue() queue.dequeue() assert test == None assert queue.data == [None, None, None, None, None, None, None, None] assert queue.size == 0 assert queue.capacity == 8 assert queue.head == 0 assert queue.tail == 0 # shrink from 9 to 4 queue = CircularQueue(9) for i in range(0, 3): queue.enqueue(i) assert queue.data == [0, 1, 2, None, None, None, None, None, None] assert queue.size == 3 assert queue.capacity == 9 assert queue.head == 0 assert queue.tail == 3 queue.dequeue() assert queue.data == [1, 2, None, None] assert queue.size == 2 assert queue.capacity == 4 assert queue.head == 0 assert queue.tail == 2 assert queue.total == 3 # test #3 from piazza student queue = CircularQueue(5) queue.enqueue(5) queue.enqueue(5) queue.enqueue(2) queue.dequeue() queue.dequeue() # [None. None, 5, None, None] queue.dequeue() assert queue.data == [None, None, None, None, None] assert queue.size == 0 assert queue.capacity == 5 assert queue.head == 0 assert queue.tail == 0 assert queue.total == 0 # test #1 from piazza student queue = CircularQueue(13) for i in range(0, 7): queue.enqueue(i) queue.dequeue() queue.dequeue() queue.dequeue() # [None, None, None, 3, 4, 5, 6, None, None, None, None, None, None] assert queue.data == [None, None, None, 3, 4, 5, 6, None, None, None, None, None, None] assert queue.size == 4 assert queue.capacity == 13 assert queue.head == 3 assert queue.tail == 7 assert queue.total == 18 queue.dequeue() assert queue.data == [4, 5, 6, None, None, None] assert queue.size == 3 assert queue.capacity == 6 assert queue.head == 0 assert queue.tail == 3 assert queue.total == 15 queue.dequeue() assert queue.data == [None, 5, 6, None, None, None] assert queue.size == 2 assert queue.capacity == 6 assert queue.head == 1 assert queue.tail == 3 assert queue.total == 11 queue.dequeue() assert queue.data == [None, None, 6, None, None, None] assert queue.size == 1 assert queue.capacity == 6 assert queue.head == 2 assert queue.tail == 3 assert queue.total == 6 # one item in large list, dequeue and then shrink queue = CircularQueue(8) queue.enqueue(1) assert queue.data == [1, None, None, None, None, None, None, None] assert queue.size == 1 assert queue.capacity == 8 assert queue.head == 0 assert queue.tail == 1 test = queue.dequeue() assert test == 1 assert queue.data == [None, None, None, None] assert queue.size == 0 assert queue.capacity == 4 assert queue.head == 0 assert queue.tail == 0 assert queue.total == 0 # possibility of adding None's to end queue = CircularQueue(10) for i in range(0, 9): queue.enqueue(i) queue.dequeue() queue.dequeue() queue.dequeue() queue.dequeue() queue.dequeue() queue.dequeue() queue.dequeue()
def setUp(self): self.cq = CircularQueue() self.cq.enqueue('A') self.cq.enqueue('B')
def main(): print("---RUNNING SAMPLE TESTCASE---") sample_test() print("---RUNNING TESTCASE #2---") test_2() print("---RUNNING TESTCASE #6---") test_6() print("My test case") queue = CircularQueue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) queue.enqueue(4) queue.dequeue() queue.dequeue() queue.enqueue(5) queue.enqueue(6) queue.enqueue(7) print(queue)
def main(): normalQueue = CircularQueue(3) vipQueue = CircularQueue(3) command = input("Add, Serve, or Exit: ") while (command != "Exit"): if command == "Add": name = input("Enter the name of the person to add: ") isVIP = input("Is the customer VIP? ") if isVIP == "True": if vipQueue.isFull(): print("Error: VIP customers queue is full") else: vipQueue.enqueue(name) statement = "add " + name + " to VIP line" print(statement) elif isVIP == "False": if normalQueue.isFull(): print("Error: Normal customers queue is full") else: normalQueue.enqueue(name) statement = "add " + name + " to the line" print(statement) else: print("Wrong Command. Please Enter True or False") #break #isVIP = input("Is the customer VIP? ") #? elif command == "Serve": if vipQueue.isEmpty() and normalQueue.isEmpty(): print("Error: Queues are empty") else: if not vipQueue.isEmpty(): served = vipQueue.dequeue() statement = served + " has been served" print(statement) else: served = normalQueue.dequeue() statement = served + " has been served" print(statement) else: print("Wrong Command. Please Enter Add, Serve or Exit") printQueues(normalQueue, vipQueue) command = input("Add, Serve, or Exit: ") print('Quiting')
def test_getHead(self): list = range(0,4) CQ = CircularQueue(list) for i in list: self.assertEqual(CQ.getHead(),i) CQ.shift()
def test_shift(self): list = range(0,4) CQ = CircularQueue(list) CQ.shift() self.assertEqual(CQ.queue,[1,2,3,0])
def counts_circularly_list(C): """ counts the number of nodes in a circularly linked list. :param C: circularly linked list :return: the number of nodes """ assert isinstance(C, CircularQueue) count = 1 # tail count 1 cursor = C._tail._next # head node while cursor is not C._tail: count += 1 cursor = cursor._next return count c = CircularQueue() for i in range(5): c.enqueue(i) print("size C:", len(c)) print("number of nodes in C:", counts_circularly_list(c)) # Suppose that x and y are references to nodes of circularly linked lists, # although not necessarily the same list. Describe a fast algorithm for telling # if x and y belong to the same list def select_node(C): """ select a random node from a circularly linked list :param C: circularly linked list :return: """ assert isinstance(C, CircularQueue)
""" Implement a function that counts the number of nodes in a circularly linked list. """ from CircularQueue import CircularQueue def count_nodes(q): quan = 0 if not q.is_empty(): top = q.first() quan += 1 q.rotate() while q.first() != top: # sad that it returns element not node quan += 1 q.rotate() return quan def count_nodes_pro(q): return len(q) q = CircularQueue() q.enqueue(1) q.enqueue(2) q.enqueue(3) q.enqueue(4) print(count_nodes(q))
def test_enqueue(self): # if item to add is None don't increase size` queue = CircularQueue() queue.enqueue(None) assert queue.data == [None, None, None, None] assert queue.size == 0 assert queue.head == 0 assert queue.tail == 0 assert queue.capacity == 4 # Normal enqueue queue = CircularQueue() queue.enqueue(10) queue.enqueue(20) queue.enqueue(30) assert queue.data == [10, 20, 30, None] assert queue.size == 3 assert queue.head == 0 assert queue.tail == 3 assert queue.capacity == 4 # mixture of last two tests queue = CircularQueue() queue.enqueue(10) queue.enqueue(None) queue.enqueue(30) assert queue.data == [10, 30, None, None] assert queue.size == 2 assert queue.head == 0 assert queue.tail == 2 assert queue.capacity == 4 # simple grow test queue = CircularQueue(5) queue.enqueue(10) queue.enqueue(20) queue.enqueue(30) queue.enqueue(40) queue.enqueue(50) assert queue.data == [10, 20, 30, 40, 50, None, None, None, None, None] assert queue.size == 5 assert queue.head == 0 assert queue.tail == 5 assert queue.capacity == 10
def __init__(self): self.queue = CircularQueue()
class TestCircularQueue(unittest.TestCase): def setUp(self): self.cq = CircularQueue() self.cq.enqueue('A') self.cq.enqueue('B') def test_instantiation(self): print('Can create an instance') self.assertIsInstance(self.cq, CircularQueue) def test_is_empty_method(self): print('Can check if the queue is empty') self.assertEqual(len(self.cq), 2) self.assertNotEqual(len(self.cq), 4) def test_first_method(self): print('Can return the first element from the front of the queue') self.assertEqual(self.cq.first(), 'A') def test_enqueue_method(self): print('Can add element to the back of the queue') self.cq.enqueue('C') self.assertEqual(len(self.cq), 3) def test_dequeue_method(self): print('Can remove element from the queue') self.cq.dequeue() self.cq.dequeue() self.assertEqual(self.cq.is_empty(), True) def test_rotate_method(self): print('Can rotate front element to the back of the queue') self.cq.rotate() self.assertEqual(self.cq.first(), 'B') def test_exception_raising(self): print('Can raise exception while performing actions on empty queue') self.cq.dequeue() self.cq.dequeue() with self.assertRaises(Empty): self.cq.first() self.cq.dequeue()