Exemple #1
0
def sample_dll():
    """Create testing dlls."""
    from dll import DoublyLinkedList
    one_dll = DoublyLinkedList([1])
    empty_dll = DoublyLinkedList()
    new_dll = DoublyLinkedList([1, 2, 3, 4, 5])
    return one_dll, empty_dll, new_dll
Exemple #2
0
def test_dll_remove():
    """Test for dll remove."""
    from dll import DoublyLinkedList
    one_dll, empty_dll, new_dll = sample_dll()
    new_dll.remove(3)
    assert new_dll.length == 4
    assert new_dll.head_node.next_node.next_node.contents == 2
    assert new_dll.head_node.next_node.next_node.previous_node.contents == 4
    try:
        new_dll.remove(10)
    except NameError:
        assert True
    new_dll.remove(5)
    assert new_dll.head_node.contents == 4
    new_dll.remove(1)
    assert new_dll.tail_node.contents == 2
    empty_dll = DoublyLinkedList()
    try:
        empty_dll.remove(100)
    except NameError:
        assert True
    one_dll = DoublyLinkedList([1])
    one_dll.remove(1)
    assert one_dll.head_node is None
    assert one_dll.tail_node is None
    assert one_dll.length == 0
Exemple #3
0
 def breadth_first(self, starting_point=None):
     """
     This internal method is a generator that will output breadth first
     traversal of a binary tree(left child, right child, parent),
     one value at a time.
     """
     if self.length == 0:
         raise IndexError("You can't breadth-first traverse an empty Tree.")
     from dll import DoublyLinkedList
     unvisited = DoublyLinkedList()
     if starting_point is None:
         starting_point = self.root
     elif self.contains(starting_point) is False:
         raise IndexError('Starting point is not in the Tree.')
     unvisited.push(starting_point)
     visited = []
     while unvisited.size() > 0:
         current = unvisited.shift()
         if current not in visited:
             visited.append(current)
             if current.left:
                 unvisited.push(current.left)
             if current.right:
                 unvisited.push(current.right)
             yield current.val
Exemple #4
0
def test_append_non_empty():
    """Assert node gets appended to the end of the lsit."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    my_list.insert(4)
    my_list.append(8)
    assert my_list.tail.val == 8
Exemple #5
0
def test_assert_previous():
    """Assert second node points to its previous."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    my_list.insert('Daniel')
    my_list.insert('Diana')
    assert my_list.head.val == 'Diana'
Exemple #6
0
def test_insert_on_non_emtpy_list():
    """Assert insert on non-empty list works."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    my_list.insert(4)
    my_list.insert('Diana')
    assert my_list.head.val == 'Diana'
Exemple #7
0
def test_DLL_shift(mk_dll):
    zeroth = DoublyLinkedList()
    with pytest.raises(IndexError):
        zeroth.shift()
    populated = mk_dll
    for x in range(19, -1, -1):
        assert populated.shift() == x
Exemple #8
0
def test_pop_non_empty():
    """Assert first node gets removed and returned."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    my_list.insert(4)
    my_list.insert(5)
    assert my_list.pop() == 5
Exemple #9
0
def test_pop_single_item_list():
    """Test pop on single item list."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    my_list.insert(4)
    my_list.pop()
    assert my_list.head is None
Exemple #10
0
def test_tail():
    """Assert the last node is the tail."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    my_list.insert(4)
    my_list.insert(5)
    my_list.insert(6)
    assert my_list.tail.val == 4
Exemple #11
0
def test_shift():
    """Assert shift works on non empty list."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    my_list.insert(1)
    my_list.insert(2)
    my_list.insert(3)
    assert my_list.shift() == 1
Exemple #12
0
def test_DLL_pop(mk_dll):
    zeroth = DoublyLinkedList()
    with pytest.raises(IndexError):
        zeroth.pop()
    populated = mk_dll
    for twice in range(2):
        for x in range(20):
            assert populated.pop() == x
Exemple #13
0
def test_remove():
    """Test if selected Node is removed from list."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    my_list.insert('last')
    my_list.insert('second')
    my_list.insert('head')
    my_list.remove('second')
    assert my_list.head.next.val == 'last'
 def __init__(self, initial_text=None):
     # the contents of our buffer are stored as a linked list
     self.contents = DoublyLinkedList()
     """  
      if we are passed in initial text as an argument
      loop through the string and make linked list nodes
      with every letter 
      """
     if initial_text:
         for char in initial_text:
             self.contents.add_to_tail(char)
Exemple #15
0
    def breadth_first_traversal(self, starting_point):
        """Steps through the graph breadth-first.

        Expects a starting point == value of a node in the graph."""
        from dll import DoublyLinkedList
        dll = DoublyLinkedList()
        if self.has_node(starting_point) is False:
            raise IndexError("That starting point is not in the graph.")
        dll.push(starting_point)
        result = []
        while dll.size() > 0:
            working_node = dll.shift()
            if working_node not in result:
                result.append(working_node)
                for node in self.neighbors(working_node):
                    dll.push(node)
        return result
Exemple #16
0
def test_DLL_app():
    dbl = DoublyLinkedList()
    dbl.append('append')
    assert dbl.head.data == 'append'
    assert dbl.tail.data == 'append'
    assert dbl.head.next is None
    assert dbl.head.prev is None
    assert dbl.tail.next is None
    assert dbl.tail.prev is None

    dbl.append('next')
    assert dbl.head.data == 'append'
    assert dbl.tail.data == 'next'
    assert dbl.tail.prev.next.data == 'next'

    dbl.append('final')
    assert dbl.head.data == 'append'
    assert dbl.tail.data == 'final'
    assert dbl.tail.prev.next.data == 'final'
Exemple #17
0
def test_DLL_ins():
    dbl = DoublyLinkedList()
    # First case with an empty list
    dbl.insert('insert')
    assert dbl.head.data == 'insert'
    assert dbl.tail.data == 'insert'
    assert dbl.head.next is None
    assert dbl.head.prev is None
    assert dbl.tail.next is None
    assert dbl.tail.next is None

    dbl.insert('next')
    assert dbl.head.data == 'next'
    # Test for updating of prev in in the second item
    assert dbl.head.next.prev.data == 'next'
    assert dbl.tail.data == 'insert'

    dbl.insert('final')
    assert dbl.head.data == 'final'
    assert dbl.head.next.prev.data == 'final'
    assert dbl.tail.data == 'insert'
Exemple #18
0
def test_DLL_rm(mk_dll):
    zeroth = DoublyLinkedList()
    with pytest.raises(IndexError):
        zeroth.remove(3)
    populated = mk_dll
    # Remove from the head
    assert populated.head.data == 0
    populated.remove(0)
    assert populated.head.data == 1
    # Remove from the tail
    assert populated.tail.data == 19
    populated.remove(19)
    assert populated.tail.data == 18
    # Remove from the middle
    current = populated.head
    for a in range(5):
        current = current.next
    the_nexts_value = current.next.data
    populated.remove(the_nexts_value)
    assert the_nexts_value + 1 == current.next.data

    # Try to remove a value that isn't there
    with pytest.raises(ValueError):
        populated.remove('not there')
 def __init__(self):
   self.size = 0
   # using DLL for constant time enqueue and dequeue
   self.dll = DoublyLinkedList()
Exemple #20
0
 def __init__(self):
     self.size = 0
     # Why is our DLL a good choice to store our elements?
     # we will be adding and subtracting based on LIFO
     self.storage = DoublyLinkedList()
Exemple #21
0
 def __init__(self, iterable=None):
     """Construct queue."""
     try:
         self._dll = DoublyLinkedList(iterable)
     except ValueError:
         raise ValueError("Queue optional parameter must be iterable.")
 def __init__(self):
     self.size = 0
     # Why is our DLL a good choice to store our elements?
     # it has directional pointers so we know
     # where something is in the 'queue'
     self.storage = DoublyLinkedList()
Exemple #23
0
 def __init__(self):
     """Initilizer of deque class."""
     self.container = DoublyLinkedList()
Exemple #24
0
 def __init__(self, iterable=None):
     """Init deque, iterate through data if provided as an argument."""
     self.dll = DoublyLinkedList(iterable)
Exemple #25
0
 def add(self, v):
     if (self.buckets[v[0]][0] == None):
         self.buckets[v[0]][0] = DoublyLinkedList()
     dist_dll = self.buckets[v[0]][0]
     dist_dll.push(v)
     self.count += 1
def dll_initial_2():
    from dll import DoublyLinkedList
    temp = DoublyLinkedList(TEST_DLL_INIT_DATA2)
    return temp
Exemple #27
0
 def __init__(self, limit=10):
     self.limit = limit
     self.size = 0
     self.order = DoublyLinkedList()
     # self.order = list()
     self.storage = dict()
Exemple #28
0
def test_insert_on_empty_list():
    """Assert insert works."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    my_list.insert(4)
    assert my_list.head.val == 4
Exemple #29
0
def test_pop_empty():
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    with pytest.raises(IndexError):
        my_list.pop()
Exemple #30
0
def test_append_empty():
    """Assert node gets appended to empty list."""
    from dll import DoublyLinkedList
    my_list = DoublyLinkedList()
    my_list.append(4)
    assert my_list.tail.val == 4