def calculate(self, queue1): queue2 = Queue(queue1.get_max_size()) while (queue1.get_rear() >= queue1.get_front()): temp = queue1.dequeue() if temp % 2 == 0: queue2.enqueue(temp * 5) else: queue2.enqueue(temp) return queue2
def merge_queue(queue1, queue2): lent = queue1.get_max_size() + queue2.get_max_size() merge_list2 = Queue(lent) while not merge_list2.is_full(): if not queue1.is_empty(): merge_list2.enqueue(queue1.dequeue()) if not queue2.is_empty(): merge_list2.enqueue(queue2.dequeue()) return merge_list2
def DS_queue(self): try: length = int(input(" Enter the length of your Queue: ")) except: return self.DS_queue() # Queue instantiation with the given length queue = Queue(length) while True: print( '\n [QUEUE OPERATIONS] \n\n 1. Enqueue\n 2. Dequeue\n 3. Display\n 4. Count\n 5. Back\n' ) try: selectedNumber = int(input("Select a number: ")) except: print("\nPlease enter a valid input\n") input("Press [Enter] to continue...") self.DS_queue() if selectedNumber == 1: value = int( input("Enter the value that you want to enqueue: ")) queue.enqueue(value) print("Success!") input("Press [Enter] to continue...") elif selectedNumber == 2: queue.dequeue() print("Success!") input("Press [Enter] to continue...") elif selectedNumber == 3: queue.display() input("Press [Enter] to continue...") elif selectedNumber == 4: queue.countQueue() input("Press [Enter] to continue...") elif selectedNumber == 5: self.DS_main() else: print( "The number you entered is not in the choices... Going back to the main screen instead..." ) input("Press [Enter] to continue...") self.DS_main()
def check_numbers(number_queue): #write your logic here lent = number_queue.get_max_size() solution_queue1 = Queue(lent) while not number_queue.is_empty(): data = number_queue.dequeue() count = 0 for i in range(1, 11): if data % i == 0: count += 1 if count == 10: solution_queue1.enqueue(data) return solution_queue1
def separate_boxes(box_stack): length = box_stack.get_max_size() temp_stack = Stack(length) que = Queue(length) while not box_stack.is_empty(): temp_box = box_stack.pop() if temp_box == "Red" or temp_box == "Green" or temp_box == "Blue": temp_stack.push(temp_box) else: que.enqueue(temp_box) while not temp_stack.is_empty(): temp_box = temp_stack.pop() box_stack.push(temp_box) return que
class QueueTestCase(unittest.TestCase): def setUp(self): self.emptyqueue = Queue() self.queue = Queue() for letter in "abcdefghijklmnopqrstuvwxyz": self.queue.enqueue(letter) def test_len(self): self.assertEqual(len(self.queue), 26, "Queue length not 26") self.assertEqual(len(self.emptyqueue), 0, "Empty queue length not 0") self.queue.enqueue("aa") self.assertEqual(len(self.queue), 27, "Queue length not 27 after enqueue") self.queue.dequeue() self.assertEqual(len(self.queue), 26, "Queue length not 26 after dequeue") self.queue.make_empty() self.assertEqual(len(self.queue), 0, "Queue length not 0 after make_empty") def test_str(self): letters = "abcdefghijklmnopqrstuvwxyz" string = str([letter for letter in letters]) self.assertEqual(str(self.queue), string, "Queue string not equal") self.assertEqual(str(self.emptyqueue), str([]), "Empty queue string not equal") def test_bool(self): self.assertTrue(self.queue, "Queue unexpectedly false") self.assertFalse(self.emptyqueue, "Empty queue unexpectedly true") def test_enqueue(self): item = "aa" self.assertNotIn(item, self.queue._data) self.queue.enqueue(item) self.assertIn(item, self.queue._data) self.assertEqual(item, self.queue._data[-1]) def test_dequeue(self): self.assertEqual(self.queue.dequeue(), "a") self.assertNotIn("a", self.queue._data) with self.assertRaises(IndexError): self.emptyqueue.dequeue() def test_peek(self): self.assertEqual(self.queue.peek(), "a") self.assertIn("a", self.queue._data) with self.assertRaises(IndexError): self.emptyqueue.peek() def test_make_empty(self): self.assertTrue(self.queue) self.queue.make_empty() self.assertEqual(self.queue._data, [])
def test_queue(self): queue = Queue(5) # Check instantiation default values self.assertEqual(queue.front, -1) self.assertEqual(queue.rear, -1) self.assertEqual(queue.length, 5) self.assertEqual(queue.data, [0,0,0,0,0]) self.assertEqual(queue.count, 0) # Check if isEmpty() self.assertTrue(queue.isEmpty()) # Check if isFull() self.assertFalse(queue.isFull()) # Check if enqueue operation is working properly queue.enqueue(5) self.assertEqual(queue.data, [5,0,0,0,0]) # Check count again if incremented self.assertEqual(queue.count, 1) # Check if front and rear is incremented self.assertEqual(queue.front, 0) self.assertEqual(queue.rear, 0) # Enqueue again to check if rear is working properly queue.enqueue(6) self.assertEqual(queue.rear, 1) self.assertEqual(queue.count, 2) # Check is countQueue() is working properly self.assertEqual(queue.countQueue(), 2) # Check if dequeue() is working properly queue.dequeue() self.assertEqual(queue.data, [0,6,0,0,0]) self.assertEqual(queue.count, 1)
from DataStructures import Queue a = Queue() personas = {'Liam', 'Olivia', 'Noah', 'Emma', 'Oliver', 'Ava', 'William', 'Sophia', 'Elijah', 'Isabella', 'James', 'Charlotte', 'Benjamin', 'Amelia', 'Lucas', 'Mia', 'Mason', 'Harper', 'Ethan', 'Evelyn'} for i in personas: a.enqueue(i) print("Current waiting queue:") a.printList() for i in range(5): print(f"It is {a.poll()}'s turn") a.printList()
# dataq1 = queue1.dequeue() # merged_queue2.enqueue(dataq1) # difference -= 1 # else : # while(difference != 0) : # dataq2 = queue2.dequeue() # merged_queue2.enqueue(dataq2) # difference -= 1 # # # merged_queue2.display() # return merged_queue2 # Enqueue different values to both the queues and test your program queue1 = Queue(3) queue2 = Queue(6) queue1.enqueue(3) queue1.enqueue(6) queue1.enqueue(8) queue2.enqueue('b') queue2.enqueue('y') queue2.enqueue('u') queue2.enqueue('t') queue2.enqueue('r') queue2.enqueue('o') merged_queue = merge_queue(queue1, queue2) print("The elements in the merged queue are:") merged_queue.display()
def Breadthfirst(self): self.__setup() queue = Queue() self.finalPath = None pathFound = False temp = None if self.board[self.startRow][self.startCol].getType() == "START": queue.enqueue(self.board[self.startRow][self.startCol]) queue.getFront().getData().setVisited(True) while not queue.isEmpty() and not pathFound: temp = queue.dequeue().getData() if temp.getType() == "FINISH": pathFound = True else: if temp.getRow() > 0 and self.board[temp.getRow() - 1][temp.getCol()].getType() != "WALL"\ and self.board[temp.getRow() - 1][temp.getCol()].getVisited() == False: self.board[temp.getRow() - 1][temp.getCol() ].setVisited(True) self.board[temp.getRow() - 1][temp.getCol() ].setPrev(temp) queue.enqueue( self.board[temp.getRow() - 1][temp.getCol()]) if temp.getRow() < self.numRow - 1\ and self.board[temp.getRow() + 1][temp.getCol()].getType() != "WALL"\ and self.board[temp.getRow() + 1][temp.getCol()].getVisited() == False: self.board[temp.getRow() + 1][temp.getCol() ].setVisited(True) self.board[temp.getRow() + 1][temp.getCol() ].setPrev(temp) queue.enqueue( self.board[temp.getRow() + 1][temp.getCol()]) if temp.getCol() > 0 and self.board[temp.getRow()][temp.getCol() - 1].getType() != "WALL"\ and self.board[temp.getRow()][temp.getCol() - 1].getVisited() == False: self.board[temp.getRow()][temp.getCol() - 1].setVisited(True) self.board[temp.getRow()][temp.getCol() - 1].setPrev(temp) queue.enqueue( self.board[temp.getRow()][temp.getCol() - 1]) if temp.getCol() < self.numCol - 1\ and self.board[temp.getRow()][temp.getCol() + 1].getType() != "WALL"\ and self.board[temp.getRow()][temp.getCol() + 1].getVisited() == False: self.board[temp.getRow()][temp.getCol() + 1].setVisited(True) self.board[temp.getRow()][temp.getCol() + 1].setPrev(temp) queue.enqueue( self.board[temp.getRow()][temp.getCol() + 1]) pathfinder_gui.showQueue(temp, queue) if pathFound: self.finalPath = Stack() while temp != None: self.finalPath.push(temp) temp = temp.getPrev() pathfinder_gui.showPath(self.finalPath)
class Cal: def calculate(self, queue1): queue2 = Queue(queue1.get_max_size()) while (queue1.get_rear() >= queue1.get_front()): temp = queue1.dequeue() if temp % 2 == 0: queue2.enqueue(temp * 5) else: queue2.enqueue(temp) return queue2 que = Queue(6) que.enqueue(2) que.enqueue(5) que.enqueue(8) que.enqueue(9) que.enqueue(6) que.enqueue(4) cla = Cal() cla.calculate(que).display()
#This assignment needs DataStructures.py file in your package, you can get it from resources page from DataStructures import Queue def check_numbers(number_queue): #write your logic here lent = number_queue.get_max_size() solution_queue1 = Queue(lent) while not number_queue.is_empty(): data = number_queue.dequeue() count = 0 for i in range(1, 11): if data % i == 0: count += 1 if count == 10: solution_queue1.enqueue(data) return solution_queue1 #Add different values to the queue and test your program number_queue = Queue(5) number_queue.enqueue(13983) number_queue.enqueue(10080) number_queue.enqueue(7113) number_queue.enqueue(2520) number_queue.enqueue(2500) check_numbers(number_queue)