Пример #1
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.enqueue(t)
    list_ = list_.copy()
    while not q.is_empty():  # unlikely to happen
        new_t = q.dequeue()
        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.enqueue(new_t_child)
    return t
Пример #2
0
class SingletonTestCase(unittest.TestCase):
    '''Check whether enqueueing 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.enqueue('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 testDequeue(self):
        '''Test dequeue() on a non-empty Queue.
        '''

        front = self.queue.dequeue()
        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 dequeue().')
Пример #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 enqueueing and dequeueing several items.
        '''

        for item in range(20):
            self.queue.enqueue(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.dequeue()
            self.assertEqual(
                front, item, 'Wrong item at the front of the Queue. Found ' +
                str(front) + ' but expected ' + str(item))
            item += 1
Пример #4
0
def descendents_from_list(t, L, arity):
    ''' (Tree, list, int) -> Tree

    Populate t's descendents from L, filling them
    in in level order, with up to arity children per node.
    Then return t.

    >>> descendents_from_list(Tree(0), [1, 2, 3, 4], 2)
    Tree(0, [Tree(1, [Tree(3), Tree(4)]), Tree(2)])
    '''
    q = Queue()
    q.enqueue(t)
    L = L.copy()
    while not q.is_empty():  # unlikely to happen
        new_t = q.dequeue()
        for i in range(0, arity):
            if len(L) == 0:
                return t  # our work here is done
            else:
                new_t_child = Tree(L.pop(0))
                new_t.children.append(new_t_child)
                q.enqueue(new_t_child)
    return t
Пример #5
0
def descendents_from_list(t, L, arity):
    ''' (Tree, list, int) -> Tree

    Populate t's descendents from L, filling them
    in in level order, with up to arity children per node.
    Then return t.

    >>> descendents_from_list(Tree(0), [1, 2, 3, 4], 2)
    Tree(0, [Tree(1, [Tree(3), Tree(4)]), Tree(2)])
    '''
    q = Queue()
    q.enqueue(t)
    L = L.copy()
    while not q.is_empty(): # unlikely to happen
        new_t = q.dequeue()
        for i in range(0,arity):
            if len(L) == 0:
                return t # our work here is done
            else:
                new_t_child = Tree(L.pop(0))
                new_t.children.append(new_t_child)
                q.enqueue(new_t_child)
    return t