Exemplo n.º 1
0
def remove_batch(queue, year):
    """
    -------------------------------------------------------
    Description:
        Remove students enrolled in given year from a given queue
        Function process queue items sequentially:
        if year does not match, item is moved to rear of queue
        students removed are stored in a queue
    Use: batch_queue = remove_batch(queue,year)
    -------------------------------------------------------
    Parameters:
        queue - a queue object containing student objects (Queue)
        year - enrollment year (int)
    Returns:
        batch_queue - a queue containing students of same batch          
    -------------------------------------------------------
    """
    batch_queue = Queue()
    if isinstance(year, int):
        for _ in range(len(queue)):
            student = deepcopy(queue.remove())
            student_key = student.key()

            if str(year) in student_key:
                batch_queue.insert(student)
            else:
                queue.insert(student)
    else:
        print('Error(remove_batch): invalid input parameter type')
    return batch_queue
Exemplo n.º 2
0
def priority_merge(queue1, queue2):
    """
    -------------------------------------------------------
    Description:
        Merge two queues into one main queue
        The front of the two queues is compared, the larger one is inserted first
        The process repeats until no more objects remain in both queues
        Further details of the merging is provided in the PDF file
    Use: q3 = proirity_merge(q1,q2)
    -------------------------------------------------------
    Parameters:
        queue1 - a queue containing student objects (Queue)
        queue2 - a queue containing student objects (Queue)
    Returns:
        queue3 - a queue containing student objects (Queue)            
    -------------------------------------------------------
    """
    max_size = len(queue1) + len(queue2)

    if max_size == 0:
        queue3 = Queue()
    else:
        queue3 = Queue(max_size)

    while not (queue1.is_empty() or queue2.is_empty()):
        student1 = queue1.peek()
        student2 = queue2.peek()
        result = compare_students(student1, student2)

        if result == 2:
            queue3.insert(queue2.remove())
        elif result == 1:
            queue3.insert(queue1.remove())
        else:
            queue2.remove()
            queue3.insert(queue1.remove())

    if queue1.is_empty():
        for _ in range(len(queue2)):
            queue3.insert(queue2.remove())

    if queue2.is_empty():
        for _ in range(len(queue1)):
            queue3.insert(queue1.remove())

    return queue3
Exemplo n.º 3
0
def shred_queue(queue, size):
    """
    -------------------------------------------------------
    Description:
        shreds a queue into several equal sized queues of given size.
        Items of the queue is distributed sequentially into the minoir queues.
        If given size is invalid, print error message and return empty list
        At the end of the shredding process the input queue should be restored.  
    Use: queue_list = shred_queue(queue,size)
    -------------------------------------------------------
    Parameters:
        queue - a queue containing arbitrary items (Queue)
        size - size of each mini queue (int)
    Returns:
        queues - a list containing mini queues (list)            
    -------------------------------------------------------
    """
    queues = []
    q = deepcopy(queue)

    if isinstance(size, int) and size > 0 and size < len(q) + 1:
        i = 0
        if len(queue) % size == 0:
            num_queues = len(queue) // size
        else:
            num_queues = len(queue) // size + 1

        while i < num_queues:
            new_queue = Queue(size)

            for _ in range(size):
                if q.is_empty():
                    break
                else:
                    new_queue.insert(q.remove())

            queues.append(new_queue)
            i += 1
    else:
        print("Error(shred_queue): invalid shred size")

    return queues
Exemplo n.º 4
0
def reverse_stack(stack):
    """
    -------------------------------------------------------
    Description:
        Reverse a stack using a queue
    Use: reverse_stack(stack)
    -------------------------------------------------------
    Parameters:
        stack - a stack of items (Stack)
    Returns:
        No returns
    -------------------------------------------------------
    """
    queue = Queue()
    
    while not stack.is_empty():
        item = deepcopy(stack.pop())
        queue.insert(item)
    
    while not queue.is_empty():
        stack.push(queue.remove())

    return
Exemplo n.º 5
0
 def levelorder(self):
     items = []
     queue = Queue()
     node = self._root  # starting from the root
     queue.insert(node)  # insert it
     while not queue.is_empty():
         node = queue.remove()  # pick that node
         items.append(node._data)  # add to the list
         # if the left side is not None, there exist a left side, add the queue (going left to right_
         if node._left is not None:
             queue.insert(node._left)
         # you also want to add the right side
         if node._right is not None:
             queue.insert(node._right)
     return items
Exemplo n.º 6
0
    def __str__(self):
        """
        -------------------------------------------------------
        Description:
            Returns a string representation of bst
            Uses Breadth First Search (BFS)
        Assert: none
        Use: str(bst) or print(bst)
        -------------------------------------------------------
        Parameters:
            None
        Returns:
            output: string representation of bst (str)
        -------------------------------------------------------
        """
        q = Queue(self.MAX_SIZE)
        output = ''

        q.insert(self._root)
        level = 1

        while not q.is_empty():
            node = q.remove()

            if node is None:
                continue

            node_level = self.level(node._data)

            if node_level == level:
                output += '{} '.format(str(node._data))

            elif node_level > level:
                output = '{}\n'.format(output[:-1])
                output += '{} '.format(str(node._data))
                level += 1

            if node._left is not None:
                q.insert(node._left)

            if node._right is not None:
                q.insert(node._right)

        return output[:-1]
Exemplo n.º 7
0
    def _reflect_vertical_aux(self, node):
        queue = Queue()
        tree = BST()

        if node is None:
            return tree

        if node is not None:
            queue.insert(node)
        while not queue.is_empty():
            temp = node._left
            node._left = node._right
            node._right = temp
            node = queue.remove()
            tree.insert(node._data)
            if node._left is not None:
                queue.insert(node._left)
            if node._right is not None:
                queue.insert(node._right)

        return tree
Exemplo n.º 8
0
 def __str__(self):
     """
     -------------------------------------------------------
     Description:
         Returns a string representation of bst
         Uses Breadth First Search (BFS)
     Assert: none
     Use: str(bst) or print(bst)
     -------------------------------------------------------
     Parameters:
         None
     Returns:
         output: string representation of bst (str)
     -------------------------------------------------------
     """
     # You can all the way down print the depth (DFS depth first search) --> Stacks
     # Horizontal search, reads horizontally to prints (BFS breath first search) --> Queues
     q = Queue(self.MAX_SIZE)
     output = ''
     q.insert(self._root)
     level = 1
     # come back to this in WEEK #11
     while not q.is_empty():
         node = q.remove()
         if node is None:
             continue
         node_level = self.level(node._data)
         if node_level == level:
             output += str(node._data) + ' '
         elif node_level > level:
             output = output[:-1] + '\n'
             output += str(node._data) + ' '
             level += 1
         if node._left is not None:
             q.insert(node._left)
         if node._right is not None:
             q.insert(node._right)
     return output[:-1]
Exemplo n.º 9
0
from my_queue import Queue

q = Queue(3)
assert (q.is_empty())
assert (hasattr(q, "items"))
assert (hasattr(q, "insert"))
assert (hasattr(q, "remove"))
assert (hasattr(q, "is_empty"))

result = q.insert(5)
assert (result == True)
assert (not q.is_empty())
assert (q.__str__() == "5")

result = q.insert(7)
assert (result == True)
assert (not q.is_empty())
assert (q.__str__() == "7 5")

result = q.insert(-1)
assert (result == True)
assert (not q.is_empty())
assert (q.__str__() == "-1 7 5")

result = q.insert(20)
assert (result == False)
assert (not q.is_empty())
assert (q.__str__() == "-1 7 5")

result = q.insert(33)
assert (result == False)