class TypicalTestCase(unittest.TestCase):

    """A comprehensive tester of typical behaviour of Queue.
    """

    def setUp(self):
        """Set up an empty queue.
        """

        self.queue = Queue()

    def tearDown(self):
        """Clean up.
        """

        self.queue = None

    def testAll(self):
        """Check adding and removing several items.
        """

        for item in range(20):
            self.queue.add(item)
            self.assertFalse(
                self.queue.is_empty(),
                'Queue should not be empty after adding item ' +
                str(item))
        item = 0
        while not self.queue.is_empty():
            front = self.queue.remove()
            self.assertEqual(
                front, item,
                'Wrong item at the front of the Queue. Found ' +
                str(front) + ' but expected ' + str(item))
            item += 1
Exemplo n.º 2
0
def levelorder_visit2(t, act):
    """
    Visit BinaryTree t in level order and act on nodes as they are visited

    @param BinaryTree|None t: binary tree to visit
    @param (BinaryTree)->Any act: function to use during visit
    @rtype: None

    >>> b = BinaryTree(8)
    >>> b = insert(b, 4)
    >>> b = insert(b, 2)
    >>> b = insert(b, 6)
    >>> b = insert(b, 12)
    >>> b = insert(b, 14)
    >>> b = insert(b, 10)
    >>> def f(node): print(node.data)
    >>> levelorder_visit2(b, f)
    8
    4
    12
    2
    6
    10
    14
    """
    nodes = Queue()
    nodes.add(t)
    while not nodes.is_empty():
        next_node = nodes.remove()
        act(next_node)
        if next_node.left:
            nodes.add(next_node.left)
        if next_node.right:
            nodes.add(next_node.right)
Exemplo n.º 3
0
class TypicalTestCase(unittest.TestCase):

    """A comprehensive tester of typical behaviour of Queue.
    """

    def setUp(self):
        """Set up an empty queue.
        """

        self.queue = Queue()

    def tearDown(self):
        """Clean up.
        """

        self.queue = None

    def testAll(self):
        """Check adding and removing several items.
        """

        for item in range(20):
            self.queue.add(item)
            self.assertFalse(self.queue.is_empty(), "Queue should not be empty after adding item " + str(item))
        item = 0
        while not self.queue.is_empty():
            front = self.queue.remove()
            self.assertEqual(
                front, item, "Wrong item at the front of the Queue. Found " + str(front) + " but expected " + str(item)
            )
            item += 1
Exemplo n.º 4
0
def levelorder_visit(t, act):
    """
    Visit every node in Tree t in level order and act on the node
    as you visit it.

    @param Tree t: tree to visit in level order
    @param (Tree)->Any act: function to execute during visit

    >>> t = descendants_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7], 3)
    >>> def act(node): print(node.value)
    >>> levelorder_visit(t, act)
    0
    1
    2
    3
    4
    5
    6
    7
    """
    nodes_to_be_processed = Queue()
    nodes_to_be_processed.add(t)
    while not nodes_to_be_processed.is_empty():
        next_node = nodes_to_be_processed.remove()
        act(next_node)
        for c in next_node.children:
            nodes_to_be_processed.add(c)
Exemplo n.º 5
0
def descendants_from_list(t, list_, arity):
    """
    Populate Tree t's descendants from list_, filling them
    in in level order, with up to arity children per node.
    Then return t.

    @param Tree t: tree to populate from list_
    @param list list_: list of values to populate from
    @param int arity: maximum branching factor
    @rtype: Tree

    >>> descendants_from_list(Tree(0), [1, 2, 3, 4], 2)
    Tree(0, [Tree(1, [Tree(3), Tree(4)]), Tree(2)])
    """
    q = Queue()
    q.add(t)
    list_ = list_.copy()
    while not q.is_empty():  # unlikely to happen
        new_t = q.remove()
        for i in range(0, arity):
            if len(list_) == 0:
                return t  # our work here is done
            else:
                new_t_child = Tree(list_.pop(0))
                new_t.children.append(new_t_child)  # t.children is a list of child trees
                q.add(new_t_child)
    return t
Exemplo n.º 6
0
class SingletonTestCase(unittest.TestCase):

    """Check whether adding a single item makes it appear at the front.
    """

    def setUp(self):
        """Set up a queue with a single element.
        """

        self.queue = Queue()
        self.queue.add("a")

    def tearDown(self):
        """Clean up.
        """

        self.queue = None

    def testIsEmpty(self):
        """Test is_empty() on non-empty Queue.
        """

        self.assertFalse(self.queue.is_empty(), "is_empty returned True on non-empty Queue!")

    def testRemove(self):
        """Test remove() on a non-empty Queue.
        """

        front = self.queue.remove()
        self.assertEqual(front, "a", 'The item at the front should have been "a" but was ' + front + ".")
        self.assertTrue(self.queue.is_empty(), "Queue with one element not empty after remove().")
Exemplo n.º 7
0
def levelorder_visit(b: Union[BTNode, None], visit: Callable[[BTNode],
                                                             Any]) -> None:
    """
    Visit each node of binary tree rooted at root in level order.

    If tree rooted at root is empty, do nothing.

    >>> b = BTNode("A", BTNode("C", BTNode("B")), BTNode("D"))

    >>> def f(node): print(node.data)
    >>> levelorder_visit(b, f)
    A
    C
    D
    B
    """
    if b is None:
        pass
    else:
        q = Queue()
        q.add(b)
        while not q.is_empty():
            n = q.remove()
            visit(n)
            if n.left is not None:
                q.add(n.left)
            if n.right is not None:
                q.add(n.right)
Exemplo n.º 8
0
def levelorder_visit(t, act):
    """
    Visit every node in Tree t in level order and act on the node
    as you visit it.

    @param Tree t: tree to visit in level order
    @param (Tree)->Any act: function to execute during visit

    >>> t = descendants_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7], 3)
    >>> def act(node): print(node.value)
    >>> levelorder_visit(t, act)
    0
    1
    2
    3
    4
    5
    6
    7
    """
    q = Queue()
    q.add(t)
    while not q.is_empty():
        next_t = q.remove()
        act(next_t)
        for c in next_t.children:
            q.add(c)
Exemplo n.º 9
0
def descendants_from_list(t, list_, arity):
    """
    Populate Tree t's descendants from list_, filling them
    in in level order, with up to arity children per node.
    Then return t.

    @param Tree t: tree to populate from list_
    @param list list_: list of values to populate from
    @param int arity: maximum branching factor
    @rtype: Tree

    >>> descendants_from_list(Tree(0), [1, 2, 3, 4], 2)
    Tree(0, [Tree(1, [Tree(3), Tree(4)]), Tree(2)])
    """
    q = Queue()
    q.add(t)
    list_ = list_.copy()
    while not q.is_empty():  # unlikely to happen
        new_t = q.remove()
        for i in range(0, arity):
            if len(list_) == 0:
                return t  # our work here is done
            else:
                new_t_child = Tree(list_.pop(0))
                new_t.children.append(
                    new_t_child)  # t.children is a list of child trees
                q.add(new_t_child)
    return t
Exemplo n.º 10
0
def levelorder_visit(t, act):
    """
    Visit every node in Tree t in level order and act on the node
    as you visit it.

    @param Tree t: tree to visit in level order
    @param (Tree)->Any act: function to execute during visit

    >>> t = descendants_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7], 3)
    >>> def act(node): print(node.value)
    >>> levelorder_visit(t, act)
    0
    1
    2
    3
    4
    5
    6
    7
    """
    q = Queue()
    q.add(t)
    while not q.is_empty():
        temp = q.remove()
        act(temp)
        for c in temp.children:
            q.add(c)
Exemplo n.º 11
0
def levelorder_visit(t: Tree, act: Callable[[Tree], Any]) -> None:
    """
    Visit each node of Tree t in levelorder, and act on the nodes as they are visited

    >>> t = descendants_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7], 3)
    >>> def act(node): print(node.value)
    >>> levelorder_visit(t, act)
    0
    1
    2
    3
    4
    5
    6
    7
    """
    t: Tree

    # Adds the tree to the queue
    to_act = Queue()
    to_act.add(t)
    # While the queue is not empty
    while not to_act.is_empty():
        # Acts on the node
        t = to_act.remove()
        act(t)
        # Adds the children to the queue
        for child in t.children:
            to_act.add(child)
Exemplo n.º 12
0
def list_queue(lst: List[object], u_queue: Queue) -> None:
    """ Adds each element of the list to the stack """
    for item in lst:
        u_queue.add(item)
    while not u_queue.is_empty():
        item = u_queue.remove()
        if type(item) == list:
            for thing in item:
                u_queue.add(thing)
        else:
            print(item)
def descendants_from_list(t: Tree, list_: list, arity: int) -> Tree:
    """
    Populate Tree t's descendants from list_, filling them
    in in level order, with up to arity children per node.
    Then return t.

    >>> descendants_from_list(Tree(0), [1, 2, 3, 4], 2)
    Tree(0, [Tree(1, [Tree(3, []), Tree(4, [])]), Tree(2, [])])
    """
    q = Queue()
    q.add(t)
    list_ = list_.copy()
    while not q.is_empty():  # unlikely to happen
        new_t = q.remove()
        for i in range(0, arity):
            if len(list_) == 0:
                return t  # our work here is done
            else:
                new_t_child = Tree(list_.pop(0))
                new_t.children.append(new_t_child)
                q.add(new_t_child)
    return t
Exemplo n.º 14
0
class SingletonTestCase(unittest.TestCase):

    """Check whether adding a single item makes it appear at the front.
    """

    def setUp(self):
        """Set up a queue with a single element.
        """

        self.queue = Queue()
        self.queue.add('a')

    def tearDown(self):
        """Clean up.
        """

        self.queue = None

    def testIsEmpty(self):
        """Test is_empty() on non-empty Queue.
        """

        self.assertFalse(
            self.queue.is_empty(),
            'is_empty returned True on non-empty Queue!')

    def testRemove(self):
        """Test remove() on a non-empty Queue.
        """

        front = self.queue.remove()
        self.assertEqual(
            front, 'a',
            'The item at the front should have been "a" but was ' +
            front + '.')
        self.assertTrue(
            self.queue.is_empty(),
            'Queue with one element not empty after remove().')
Exemplo n.º 15
0
def level_order_visit(root: Union[BTNode, None], visit: Callable[["BTNode"],
                                                                 Any]) -> None:
    """
    Visit the tree in level order

    >>> b = BTNode("A", BTNode("C", BTNode("B")), BTNode("D"))
    >>> def f(node): print(node.data)
    >>> level_order_visit(b, f)
    A
    C
    D
    B
    >>> b2 = BTNode(1, BTNode(2, BTNode(4, BTNode(6)), BTNode(5)), BTNode(3, BTNode(7), BTNode(8, BTNode(9))))
    >>> level_order_visit(b2, f)
    1
    2
    3
    4
    5
    7
    8
    6
    9
    """
    node: BTNode

    if root is None:
        pass
    else:
        q = Queue()
        q.add(root)
        while not q.is_empty():
            node = q.remove()
            visit(node)
            if node.left is not None:
                q.add(node.left)
            if node.right is not None:
                q.add(node.right)
Exemplo n.º 16
0
def list_queue(lst: list, q: Queue):
    """
    Takes all elements of a given list and adds them to the queue. Then, checks
    the queue for items that are not lists and prints them. If the item being
    checked is a list, it is added to the queue. This process repeats until it
    is empty.

    >>> q = Queue()
    >>> l1 = [1, 3, 5]
    >>> l2 = [1, [3, 5], 7]
    >>> l3 = [1, [3, [5, 7], 9], 11]
    >>> list_queue(l1, q)
    1
    3
    5
    >>> list_queue(l2, q)
    1
    7
    3
    5
    >>> list_queue(l3, q)
    1
    11
    3
    9
    5
    7
    """
    for i in lst:
        q.add(i)
    while not q.is_empty():
        nxt = q.remove()
        if type(nxt) != list:
            print(nxt)
        else:
            for i in nxt:
                q.add(i)
Exemplo n.º 17
0
    7
    """
    for i in lst:
        q.add(i)
    while not q.is_empty():
        nxt = q.remove()
        if type(nxt) != list:
            print(nxt)
        else:
            for i in nxt:
                q.add(i)


if __name__ == "__main__":
    q = Queue()
    val = int(input("Enter an integer:"))
    if val == 148:
        pass
    else:
        q.add(val)
    while val != 148:
        val = int(input("Enter an integer:"))
        if val == 148:
            break
        else:
            q.add(val)
    total = 0
    while not q.is_empty():
        total += q.remove()
    print(total)
Exemplo n.º 18
0
        else:
            break
    ''' alternatively
    for i in l:
        q.add(i)
    while not q.is_empty():
        el = q.remove()
        if isinstance(el, list):
            for j in el:
                q.add(j)
        else:
            print(el)'''


if __name__ == '__main__':
    import doctest
    doctest.testmod()

    q = Queue()

    while (True):
        inp = int(input('enter an integer: '))
        if inp == 148:
            break
        q.add(inp)

    mul = 1
    while (not q.is_empty()):
        mul = q.remove() * mul
    print(mul)
Exemplo n.º 19
0
queue client module
"""
from csc148_queue import Queue
from typing import List


def list_queue(lst: List[object], u_queue: Queue) -> None:
    """ Adds each element of the list to the stack """
    for item in lst:
        u_queue.add(item)
    while not u_queue.is_empty():
        item = u_queue.remove()
        if type(item) == list:
            for thing in item:
                u_queue.add(thing)
        else:
            print(item)


if __name__ == '__main__':
    new_queue = Queue()
    new_sum = 0
    number = int(input("Enter an integer: "))
    while number != 148:
        new_queue.add(number)
        number = int(input("Enter an integer: "))
    while not new_queue.is_empty():
        print(new_queue.remove())
        # new_sum += new_queue.remove()
    # print(new_sum)
Exemplo n.º 20
0
from csc148_queue import Queue


def list_queue(l: list, q: "Queue") -> None:
    for item in l:
        q.add(item)

    # Remove sequence.
    while not q.is_empty():
        obj = q.remove()
        if type(obj) == list:
            for sub_item in obj:
                q.add(sub_item)
        else:
            print(obj)


if __name__ == "__main__":
    queue = Queue()
    input_received = None
    while not input_received == 148:
        input_received = int(input("value to be added: "))
        if not input_received == 148:
            queue.add(input_received)

    accumulator = 0
    while not queue.is_empty():
        accumulator += queue.remove()
    print(accumulator)
Exemplo n.º 21
0
            break

    ''' alternatively
    for i in l:
        q.add(i)
    while not q.is_empty():
        el = q.remove()
        if isinstance(el, list):
            for j in el:
                q.add(j)
        else:
            print(el)'''


if __name__ == '__main__':
    import doctest
    doctest.testmod()

    q = Queue()

    while(True):
        inp = int(input('enter an integer: '))
        if inp == 148:
            break
        q.add(inp)

    mul = 1
    while(not q.is_empty()):
        mul = q.remove() * mul
    print(mul)