コード例 #1
0
    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
コード例 #2
0
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()

    print(q.is_empty())
    print("inserting 5:", q.insert(5))
    print("removing", q.remove())
    print("Peek:", q.peek())
    print("Length", q.len)

    # tests for the queue methods go here
    # print the results of the method calls and verify by hand

    return None
コード例 #3
0
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()
コード例 #4
0
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
コード例 #5
0
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
コード例 #6
0
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
コード例 #7
0
ファイル: AC3.py プロジェクト: Edmund-Lui98/SudokuCSP
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
コード例 #8
0
ファイル: utilities.py プロジェクト: DoublePlay26/CP164-Notes
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