def queue_test(a): """ ------------------------------------------------------- Tests queue implementation. Tests the methods of Queue are tested for both empty and non-empty queues using the data in a: is_empty, insert, remove, peek, len Use: queue_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: None ------------------------------------------------------- """ q = Queue() array_to_queue(q, a) print(q.is_empty()) print(q.peek()) print(q.remove()) #print(q.insert(a)) # tests for the queue methods go here # print the results of the method calls and verify by hand return
def levelorder(self): """ ------------------------------------------------------- Copies the contents of the tree in levelorder order to a list. Use: values = bst.levelorder() ------------------------------------------------------- Returns: values - a list containing the values of bst in levelorder. (list of ?) ------------------------------------------------------- """ values = [] q = Queue() if self._root is not None: q.insert(self._root) while not q.is_empty(): node = q.remove() values.append(deepcopy(node._value)) if node._left is not None: q.insert(node._left) if node._right is not None: q.insert(node._right) return values
def reverse(st=Stack(4)): q = Queue() while not st.is_empty(): q.enqueue(st.peek()) st.pop() while not q.is_empty(): st.push(q.peek()) q.dequeue()
def levelorder(self): """ ------------------------------------------------------- Copies the contents of the tree in levelorder order to a list. Returns a list of the nodes in order of level from the top. Use: values = bst.levelorder() ------------------------------------------------------- Returns: values - a list containing the values of bst in levelorder. (list of ?) ------------------------------------------------------- """ a = [] if self._root is not None: queue = Queue() queue.insert(self._root._value) a, _ = self._levelorder_aux(self._root, queue) return a
def queue_test(a): """ ------------------------------------------------------- Tests queue implementation. Use: queue_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: the methods of Queue are tested for both empty and non-empty queues using the data in a: is_empty, insert, remove, peek, len ------------------------------------------------------- """ queue = Queue() dummy = [] if queue.is_empty() == True: print('Queue is empty.') array_to_queue(queue, a) print('Converting a into a queue...') if queue.is_empty() == False: print('a has been transferred into queue!') print('\nRemoving queue...') while queue.is_empty() == False: temp = queue.remove() print(temp) dummy.append(temp) print('\nqueue is empty. Inserting values back into queue...') while dummy != []: temp = dummy.pop() print(temp) queue.insert(temp) print('\nPushing complete! Peeking...') print(queue.peek()) print('\nqueue is {} objects long!'.format(len(queue))) return
def ac3(sudoku): #initialize queue q = Queue() #add all constraints to the queue for i in sudoku.constraints: q.insert(i) #go through all the constraints while q.is_empty() != True: #print("Length of queue: " + str(q.length)) xi, xj = q.remove() if revise(sudoku, xi, xj): if len(sudoku.domains[xi]) == 0: return False for xk in sudoku.neighbors[xi]: if xk != xi: q.insert([xk, xi]) return True
def queue_test(a): """ ------------------------------------------------------- Tests queue implementation. Use: queue_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: the methods of Queue are tested for both empty and non-empty queues using the data in a: is_empty, insert, remove, peek, len ------------------------------------------------------- """ q = Queue() # tests for the queue methods go here # print the results of the method calls and verify by hand # Test if queue is empty (Expected: TRUE) print("queue contents: ") for i in q: print(i, end=' ') print("\tEmpty? {}".format(q.is_empty())) # Load elements from a into queue print(">> Insert elements into a onto queue") print(">> Num elements to insert: {}".format(len(a))) for elem in a: q.insert(elem) # Check if the queue is empty again (T/F depending on a contents) print("queue contents: ") for i in q: print(i, end=' ') print("\tEmpty? {}".format(q.is_empty())) # Peek the top of the queue top = None try: top = q.peek() except: print(">>! Peeked at an empty queue, assertion caught, continuing...") # Check if top of queue is the end element of a print("Top of queue should be same as end of list") if q.is_empty(): print("List empty so no check") else: top_same = (top == a[0]) print("Top same as beginning of list?") print("Top: {}\tFront of list: {}".format(top, a[0])) print("Same? {}".format(top_same)) # Testing pop try: print(">> Remove top of queue") popped = q.remove() print("Removed value should be same as beginning of list.\tSame? {}".format(popped == a[0])) except: print(">>! Removed from an empty queue, exception caught, continuing...") return return
def queue_test(source): """ ------------------------------------------------------- Tests queue implementation. Use: test_Queue_array(source) ------------------------------------------------------- Parameters: source - list of data (list of ?) Returns: the methods of CircularQueue are tested for both is_empty and non-is_empty queues using the data in source: is_empty, push, pop, peek ------------------------------------------------------- """ q = Queue() print("Queue Initialised") print() print(SEP) print("Queue is_empty (expect True): {}".format(q.is_empty())) print("Queue size (expect 0): {}".format(len(q))) print(SEP) print("Add one item to Queue") q.insert(source[0]) print("Front of Queue (peek):") print(q.peek()) print(SEP) print("Queue is_empty (expect False): {}".format(q.is_empty())) print("Queue size (expect 1): {}".format(len(q))) print(SEP) print("Queue remove") v = q.remove() print(v) print(SEP) print("Queue is_empty (expect True): {}".format(q.is_empty())) print("Queue size (expect 0): {}".format(len(q))) print(SEP) print("Copy all data to Queue") array_to_queue(q, source) print("Queue is_empty (expect False): {}".format(q.is_empty())) print("Queue size (expect > 0): {}".format(len(q))) print(SEP) print("Front of Queue (peek):") print(q.peek()) print(SEP) print("Remove all elements from Queue") while not q.is_empty(): v = q.remove() print(v) print() print(SEP) print("Queue is_empty (expect True): {}".format(q.is_empty())) print("Queue size (expect 0): {}".format(len(q))) print() return
................................................... CP164 - Data Structures Author: Laith Adi ID: 170265190 Email: [email protected] Updated: 2019-02-10 ................................................... ''' from Queue_array import Queue target = Queue() target2 = Queue() target2.insert(1) target2.insert(1) target2.insert(1) target2.insert(1) target2.insert(1) target2.insert(1) target2.insert(1) target.insert(1) target.insert(1) target.insert(1) target.insert(1) target.insert(2) target.insert(1) target.insert(1)
''' ................................................... CP164 - Data Structures Author: Laith Adi ID: 170265190 Email: [email protected] Updated: 2019-02-10 ................................................... ''' from Queue_array import Queue from functions import queue_is_identical source1 = Queue() source2 = Queue() source1.insert(11) source1.insert(21) source1.insert(31) source2.insert(11) source2.insert(21) source2.insert(31) print(source1._values) print(source2._values) identical = queue_is_identical(source1, source2) print(identical)
from definedsizestack import Stack from Queue_array import Queue def reverse(q=Queue()): st = Stack() while not q.is_empty(): st.push(q.peek()) q.dequeue() while not st.is_empty(): q.enqueue(st.peek()) st.pop() s = Queue() s.enqueue(11) s.enqueue(22) s.enqueue(33) s.display() reverse(s) s.display()
def main(): q = Queue() source = [1,2,3] array_to_queue(q, source) for v in q: print(v)
def queue_test(a): """ ------------------------------------------------------- Tests queue implementation. Use: queue_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: the methods of Queue are tested for both empty and non-empty queues using the data in a: is_empty, insert, remove, peek, len ------------------------------------------------------- """ q = Queue() print('Test is empty?') print(q.is_empty()) print(array_to_queue(q, a)) print('Test is empty?') print(q.is_empty()) print('Test for peek value: ') print(q.peek()) print('Test for push value: ') q.insert(1) print('Test for peek value: ') print(q.peek()) print('Test for pop value: ') q.remove() print('Test is empty?') print(q.is_empty()) print('Test for peek value: ') print(q.peek()) return