def test_dll(fin):
    for cmd in fin:
        cmd = cmd.strip().split()
        if cmd[0] == "new":
            dll = DLL()
            test_print(dll)
        elif cmd[0] == "insert":
            test_insert(dll, int(cmd[1]))
        elif cmd[0] == "remove":
            test_remove(dll)
        elif cmd[0] == "move_to_next":
            test_move_to_next(dll)
        elif cmd[0] == "move_to_prev":
            test_move_to_prev(dll)
        elif cmd[0] == "move_to_pos":
            test_move_to_pos(dll, int(cmd[1]))
        elif cmd[0] == "get_first_node":
            test_get_first_node(dll)
        elif cmd[0] == "get_last_node":
            test_get_last_node(dll)
        elif cmd[0] == "clear":
            test_clear(dll)
        elif cmd[0] == "sort":
            test_sort(dll)
        elif cmd[0] == "partition":
            test_partition(dll)
Exemplo n.º 2
0
def test_doubly_linked_list_constructor_with_iterable(itr):
    """Test that a DLL created with an iterable contains all items."""
    from dll import DLL
    l = DLL(itr)
    assert l.head.val == itr[-1]
    assert l.tail.val == itr[0]
    assert l.length == len(itr)
Exemplo n.º 3
0
def test_remove_first_instance_of_value_from_doubly_linked_list(itr):
    """Test that removing any node from any length list adjusts the list."""
    from dll import DLL
    l = DLL(itr)
    l.remove(1)
    assert l.head.val == 0
    assert l.head.nxt.val == 0
    assert l.head.nxt.nxt.val == 1
    assert l.length == len(itr) - 1
Exemplo n.º 4
0
def test_remove_head_of_doubly_linked_list(itr):
    """Test that removing the head from any length list moves the head."""
    from dll import DLL
    l = DLL(itr)
    l.remove(itr[-1])
    assert l.head.val == itr[-2]
    assert l.length == len(itr) - 1
    assert l.tail.val == itr[0]
    assert l.head.prev is None
Exemplo n.º 5
0
 def test_search(self):
     dll = DLL()
     # Test an empty Linked List
     assert dll.search(5) == 'Linked List is empty.'
     # Test Linked List with some nodes
     dll.addNode(1)
     dll.addNode(2)
     dll.addNode(3)
     assert dll.search('python') == False
     assert dll.search(3) == True
Exemplo n.º 6
0
def test_remove_tail_from_doubly_linked_list(itr):
    """Test that removing tail from any length list adjusts the list."""
    from dll import DLL
    l = DLL(itr)

    l.remove(itr[0])

    assert l.tail.val == itr[1]
    assert l.tail.nxt is None
    assert l.length == len(itr) - 1
Exemplo n.º 7
0
def test_shift_one_item_from_any_length_doubly_linked_list(itr):
    """Test that shift item removes head from DLL."""
    from dll import DLL
    l = DLL(itr)
    x = l.shift()
    assert x == itr[0]
    assert l.head.val == itr[-1]
    assert l.tail.val == itr[1]
    assert l.tail.nxt is None
    assert l.length == len(itr) - 1
Exemplo n.º 8
0
def test_pop_one_item_from_any_length_doubly_linked_list(itr):
    """Test that pop item removes head from DLL."""
    from dll import DLL
    l = DLL(itr)
    x = l.pop()
    assert x == itr[-1]
    assert l.head.val == itr[-2]
    assert l.head.prev is None
    assert l.tail.val == itr[0]
    assert l.length == len(itr) - 1
Exemplo n.º 9
0
 def test_size(self):
     dll = DLL()
     size = dll.size()
     # Test an empty Linked List
     assert size == 0
     # Test Linked List with some nodes
     dll.addNode(1)
     dll.addNode(2)
     dll.addNode(3)
     size = dll.size()
     assert size == 3
Exemplo n.º 10
0
def test_remove_random_inner_node_from_doubly_linked_list(itr):
    """Test that removing any node from any length list adjusts the list."""
    from dll import DLL
    from random import choice
    l = DLL(itr)

    remove_item = choice(itr[1:-1])
    l.remove(remove_item)
    with pytest.raises(ValueError):
        l.remove(remove_item)
    assert l.length == len(itr) - 1
Exemplo n.º 11
0
    def __init__(self, id, load_function, MAX_SIZE, TIMEOUT):
        '''
		params:
		    id: unique identifier
		    load_function: if there's a cache miss, use this function to load
		    		   the value associated with a given key
		    MAX_SIZE: int, maximum number of items the ServerNode can hold
		    TIMEOUT: int/float, seconds until a node times out from unuse
		'''
        self.id = id
        self.load_function = load_function
        self.nodes = DLL(MAX_SIZE, TIMEOUT)
Exemplo n.º 12
0
 def test_addNode(self):
     dll = DLL()
     dll.head
     dll.addNode(1)
     assert str(dll.head) == 'DLLNode object: data=1'
     assert dll.head.previous == None
     assert dll.head.next == None
     dll.addNode(2)
     assert str(dll.head) == 'DLLNode object: data=2'
     assert dll.head.previous == None
     assert str(dll.head.next) == 'DLLNode object: data=1'
     assert dll.size() == 2
     assert dll.head.next.next == None
Exemplo n.º 13
0
def test_shift_multiple_items_from_any_length_doubly_linked_list(itr):
    """Test that shift items removes head from DLL."""
    from dll import DLL
    from random import randint
    l = DLL(itr)
    num = randint(2, len(itr) - 1)
    for _ in range(num):
        x = l.shift()
    assert x == itr[num - 1]
    assert l.head.val == itr[-1]
    assert l.tail.val == itr[num]
    assert l.tail.nxt is None
    assert l.length == len(itr) - num
Exemplo n.º 14
0
def parenthetics(string):
    """The parenthetics module checks for open, broken, or balanced sets."""
    matches = DLL()
    for char in string:
        if char == "(":
            matches.append(char)
        if char == ")":
            if matches.head is None or matches.pop() != "(":
                return -1
    if matches.head is None:
        return 0
    else:
        return 1
Exemplo n.º 15
0
def test_remove(test_empty_DLL, test_DLL):
    with pytest.raises(ValueError):
        test_empty_DLL.remove(3)
    test_DLL.remove(2)
    assert test_DLL.to_string() == '(3, 1)'
    test_DLL.remove(3)
    assert test_DLL.to_string() == '(1)'
    test_DLL.remove(1)
    assert test_DLL.to_string() == '()'
    long_DLL = DLL([5, 5, 6, 7, 4, 6, 0])
    long_DLL.remove(5)
    assert long_DLL.to_string() == '(0, 6, 4, 7, 6, 5)'
    long_DLL.remove(6)
    assert long_DLL.to_string() == '(0, 4, 7, 6, 5)'
Exemplo n.º 16
0
 def test_remove(self):
     dll = DLL()
     # Test an empty Linked List
     assert str(
         dll.remove(5)) == 'Linked List is empty. No Nodes to remove.'
     # Test Linked List with some nodes
     dll.addNode(1)
     dll.addNode(2)
     assert str(dll.remove(24)) == 'A Node with given data is not present.'
     assert dll.search(2) == True
     dll.remove(2)
     assert dll.search(2) == False
     dll.addNode(3)
     assert dll.size() == 2
     assert str(dll.head) == 'DLLNode object: data=3'
     dll.remove(3)
     assert str(dll.head) == 'DLLNode object: data=1'
Exemplo n.º 17
0
def findPairsWithSumX(head, tail, x):
    result = []
    if head is None or tail is None:
        return result
    while head != tail and tail.next != head:
        if head.data + tail.data == x:
            result.append((head.data, tail.data)
            head, tail = head.next, tail.prev
        elif head.data + tail.data < x:
            head = head.next
        else:
            tail = tail.prev
    return result

s = [1, 2, 4, 5, 6, 8, 9]
dll = DLL()
for i in s:
    dll.append(i)
dll.printList()
print findPairsWithSumX(dll.head, dll.tail, 7)
Exemplo n.º 18
0
def test_dll(fin):
    for cmd in fin:
        cmd = cmd.strip().split()
        if cmd[0] == "new":
            dll = DLL()
            test_print(dll)
        elif cmd[0] == "insert":
            test_insert(dll, int(cmd[1]))
        elif cmd[0] == "remove":
            test_remove(dll)
        elif cmd[0] == "move_to_next":
            test_move_to_next(dll)
        elif cmd[0] == "move_to_prev":
            test_move_to_prev(dll)
        elif cmd[0] == "move_to_pos":
            test_move_to_pos(dll, int(cmd[1]))
        elif cmd[0] == "remove_all":
            test_remove_all(dll, int(cmd[1]))
        elif cmd[0] == "sort":
            test_sort(dll)
        elif cmd[0] == "reverse":
            test_reverse(dll)
Exemplo n.º 19
0
 def __init__(self, iter=None):
     self._container = DLL()
     if iter:
         for val in iter:
             self._container.append(val)
Exemplo n.º 20
0
 def setUp(self):
     self.myDll = DLL(badDataSource)
Exemplo n.º 21
0
 def setUp(self):
     self.myDll = DLL(nullDataSource)
Exemplo n.º 22
0
def main():

    orig_stdout = sys.stdout
    fout = open('out.txt', 'w+')
    sys.stdout = fout

    print("\n\nTESTING THE BASIC STUFF\n")

    dll = DLL()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("A")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("B")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("C")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("D")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("E")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_next()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_next()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("1")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("2")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_next()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("3")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("4")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_prev()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("VALUE")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_pos(8)
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_pos(2)
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_prev()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_prev()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_prev()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_prev()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_prev()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_prev()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_next()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_next()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_next()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_next()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_pos(-1)
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_pos(18)
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_pos(0)
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))

    print("\n\nTESTING MORE COMPLEX STUFF\n")

    dll = DLL()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("A")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("B1")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("C")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("A")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("B2")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.reverse()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("C")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("A")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("B3")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.insert("C")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))

    dll.remove_all("C")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.sort()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))

    dll.remove_all("A")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))

    dll.reverse()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))

    dll.remove_all("C")
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))

    dll.move_to_next()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_prev()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.move_to_prev()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))
    dll.remove()
    print(
        str(dll) + "   -   current value: " + str(dll.get_value()) +
        "   -   size: " + str(len(dll)))

    sys.stdout = orig_stdout
    fout.close()
Exemplo n.º 23
0
def btToDll(root):
    result = DLL()
    inorder(root, result)
    return result
Exemplo n.º 24
0
def test_empty_DLL():
    return DLL()
Exemplo n.º 25
0
def test_shift_single():
    test_DLL = DLL([1])
    assert test_DLL.shift() == 1
    assert test_DLL.shift() is None
Exemplo n.º 26
0
def test_pop_single():
    test_DLL = DLL([1])
    assert test_DLL.pop() == 1
    assert test_DLL.pop() is None
Exemplo n.º 27
0
def test_init_DLL():
    """Testing the __init__ method of DLL object."""
    assert isinstance(DLL(), DLL)
    assert isinstance(DLL([1, 2, 3]), DLL)
    test_DLL = DLL()
    assert test_DLL.tail is None
Exemplo n.º 28
0
    while current and current.next:
        if current.next.data > newNode.data:
            break
        current = current.next
    if current is None:
        dll.head = newNode
    elif current.next is None:
        current.next = newNode
        newNode.prev = current
        dll.tail = newNode
    else:
        newNode.next = current.next
        newNode.prev = current
        newNode.next.prev = newNode
        current.next = newNode


dll = DLL()
for i in range(7):
    dll.append(2 * i + 1)
dll.printList()
sortedInsert(dll, 0)
dll.printList()
sortedInsert(dll, 4)
dll.printList()
sortedInsert(dll, 16)
dll.printList()
sortedInsert(dll, 0)
dll.printList()
sortedInsert(dll, 7)
dll.printList()
Exemplo n.º 29
0
 def setUp(self):
     self.dll = DLL()
Exemplo n.º 30
0
def test_DLL():
    return DLL([1, 2, 3])