예제 #1
0
def analyseCountFunctionOfDoublyLinkedList(iterable, word):
    _sum = 0
    doublyLinkedListHistogram = doubly_linked_list.DoublyLinkedList(iterable)
    for i in range(100):
        _sum += doublyLinkedListHistogram.find(lambda node: node[0] == word)
    avg = _sum / 100
    return avg
    def setUp(self):

        self.test_list = doubly_linked_list.DoublyLinkedList()

        for each_number in range(1, 10):
            self.test_list.insert(each_number)

        self.test_list_for_one_node = doubly_linked_list.DoublyLinkedList()
        self.test_list_for_one_node.insert("one node list test data")

        self.test_list_for_string_data = doubly_linked_list.DoublyLinkedList()
        self.test_list_for_string_data.insert('TestData')
        self.test_list_for_string_data.insert(61)
        self.test_list_for_string_data.insert("Additional Test Data")

        self.test_list_for_no_nodes = doubly_linked_list.DoublyLinkedList()
def test_remove():
    """remove(val) should remove the first node with the given val from the list, wherever it might be
    if node is not an item in the list, raises IndexError"""
    dll = d.DoublyLinkedList()
    dll.insert(1)
    dll.insert(2)
    dll.insert(3)

    with pytest.raises(TypeError):
        dll.remove()
        dll.remove("adfe", 5)
    with pytest.raises(IndexError):
        dll.remove(8)

    assert dll.remove(2) is None
    assert dll.search(2) is None
    assert dll.search(3).next.content == 1  # i.e., 3 goes to 1 now
    dll.remove(1)
    assert dll.head.content == 3
    assert dll.tail.content == 3
    assert dll.head.next is None
    assert dll.head is dll.tail
    assert dll.tail.previous is None
    dll.remove(3)
    assert dll.head is None
    assert dll.tail is None
예제 #4
0
def analyseGenerationOfDoublyLinkedList(iterable):
    start_time = time.time()
    _sum = 0
    for i in range(100):
        histogram = doubly_linked_list.DoublyLinkedList(iterable)
        _sum += time.time() - start_time
    avg = _sum / 100
    return avg
예제 #5
0
 def test_set_tail_implementation(self):
     first_node = doubly_linked_list.Node(1)
     second_node = doubly_linked_list.Node(2)
     last_node = doubly_linked_list.Node(3)
     dbLList = doubly_linked_list.DoublyLinkedList()
     dbLList.setHead(first_node)
     dbLList.setHead(second_node)
     dbLList.setTail(last_node)
     self.assertEqual(dbLList.length, 3)
예제 #6
0
class LRUCache:
    '''
    LRUCache represents cache with LRU Heuristic
    '''

    capacity = 0
    doubly_list = dll.DoublyLinkedList()
    node_map = {}

    def __init__(self, capacity):
        '''
        Initliaze the cache with fixed capacity
        '''
        self.capacity = capacity

    def get(self, key):
        '''
        Get the value of the key if present else -1
        '''
        if key in self.node_map:
            node = self.node_map[key]
            # Delete the Node to move it to front of the list
            self.doubly_list.delete(node)
            # Add it back again
            self.doubly_list.insert(node)
            return node.pair[1]

        return -1

    def put(self, key, value):
        '''
        Put the key, value pair in the cache, if cache is full, delete the least
        recently used value
        '''
        if key in self.node_map:
            node = self.node_map[key]
            # Delete the Node to move it to front of the list
            self.doubly_list.delete(node)
            # Update the value of the node
            node.pair = (key, value)
            # Add it back to the Doubly Linked List
            self.doubly_list.insert(node)
            self.node_map[key] = node
        else:
            # LRU has reached the capacity, so we need to delete the least recently used value
            if len(self.node_map) == self.capacity:
                deleted = self.doubly_list.delete_last()
                self.node_map.pop(deleted.pair[0])

            # Add the new value
            node = dll.Node((key, value))
            self.node_map[key] = node
            self.doubly_list.insert(node)

    def __repr__(self):
        return repr(self.doubly_list)
예제 #7
0
 def test_set_head_implementation(self):
     first_node = doubly_linked_list.Node(1)
     second_node = doubly_linked_list.Node(2)
     dbLList = doubly_linked_list.DoublyLinkedList()
     dbLList.setHead(first_node)
     dbLList.setHead(second_node)
     firstNode_Val = dbLList.get(0)
     secondNode_Val = dbLList.get(1)
     self.assertEqual(dbLList.length, 2)
     self.assertEqual(second_node.val, firstNode_Val)
     self.assertEqual(first_node.val, secondNode_Val)
    def test_DoublyLinkedList(self):

        self.setUp()

        # This feels pretty superfluous due to setUp, which appears to
        # be intended to come first, but here it is anyways.
        # We'll say it's for future-proofing.

        constructor_test_object = doubly_linked_list.DoublyLinkedList()
        self.assertTrue(
            isinstance(constructor_test_object,
                       doubly_linked_list.DoublyLinkedList))
def test_append():
    """append(val) will append the value 'val' at the tail of the list"""
    dll = d.DoublyLinkedList()
    with pytest.raises(TypeError):
        dll.append()
    # returns nothing, we have no way of getting at stuff yet
    assert dll.append("blaguboo") is None
    assert dll.head.content == "blaguboo"
    assert dll.tail.content == "blaguboo"
    dll.append("one thing")
    dll.append("second thing")
    assert dll.tail.content == "second thing"
    assert dll.head.content == "blaguboo"
def test_insert():
    """insert(val) will insert the value 'val' at the head of the list"""
    dll = d.DoublyLinkedList()
    with pytest.raises(TypeError):
        dll.insert()

    #returns nothing
    assert dll.insert("blaguboo") is None
    assert dll.head.content == "blaguboo"
    assert dll.tail.content == "blaguboo"
    dll.insert("one thing")
    dll.insert("second thing")
    assert dll.head.content == "second thing"
    assert dll.tail.content == "blaguboo"
def test_shift():
    """shift() will remove the last value from the tail of the list and return it."""
    dll = d.DoublyLinkedList()
    with pytest.raises(IndexError) as err:
        dll.shift()
        assert "empty" in err.value
    dll.insert(10)
    dll.insert(9)
    assert dll.shift() == 10
    assert dll.shift() == 9
    assert dll.head is None
    assert dll.tail is None
    with pytest.raises(IndexError) as err:
        dll.pop()
        assert "empty" in err.value
def test_pop():
    '''Should return head data & remove it, raise IndexError if empty'''
    dll = d.DoublyLinkedList()
    with pytest.raises(IndexError) as err:
        dll.pop()
        assert "empty" in err.value
    dll.insert(10)
    dll.insert(9)
    assert dll.pop() == 9
    assert dll.pop() == 10
    assert dll.head is None
    assert dll.tail is None
    with pytest.raises(IndexError) as err:
        dll.pop()
        assert "empty" in err.value
예제 #13
0
 def __init__(self):
     self.size = 0
     self.items = dll.DoublyLinkedList()
예제 #14
0
 def setUp(self):
     self.list = doubly_linked_list.DoublyLinkedList()
def test_create():
    """Should raise TypeError if initialized with data, return a doubly linked list otherwise"""
    with pytest.raises(TypeError):
        d.DoublyLinkedList("a")
    assert isinstance(d.DoublyLinkedList(), d.DoublyLinkedList)
예제 #16
0
 def __init__(self):
     self.mem = doubly_linked_list.DoublyLinkedList()
예제 #17
0
 def test_empty_list_when_init_doubly_linked_list(self):
     dbLList = doubly_linked_list.DoublyLinkedList()
     self.assertEqual(dbLList.length, 0)
     self.assertEqual(dbLList.head, None)
     self.assertEqual(dbLList.tail, None)
예제 #18
0
 def test_insert_before_implementation(self):
     node_insert = doubly_linked_list.Node(1)
     dbLList = doubly_linked_list.DoublyLinkedList()
 def setUp(self):
     self.SUT = doubly_linked_list.DoublyLinkedList(
         [4, 1, 6, 0, 12, 7, 13, 5])
예제 #20
0
#      - Traverse.
#      - Push/Append/Insert nodes.
#      - Remove by data/index.
#      - Delete.
"""
#################################################
# ###  Author: Samyuel Danyo
# ###  Date: 31/05/2020
# ###  Last Edit: 31/05/2020
##################################################
# Local Application/Library Specific Imports.
import doubly_linked_list as dll
##################################################
# Test
##################################################
DLLIST = dll.DoublyLinkedList(dll.DLLNode(0))
for i in range(1, 6):
    DLLIST.insert(i, i)
DLLIST.push(-1)
DLLIST.append(7)
DLLIST.insert_after(DLLIST.head.next, 100)
DLLIST.insert(5, 100)
DLLIST.traverse()
print('=' * 30)
DLLIST.remove(100)
print(DLLIST)
print('=' * 30)
DLLIST.remove_node(3)
DLLIST.traverse()
print('=' * 30)
DLLIST.delete()
예제 #21
0
 def __init__(self):
     self.size = 0
     # Why is our DLL a good choice to store our elements?
     self.storage = doubly_linked_list.DoublyLinkedList()
예제 #22
0
 def __init__(self, array=None):
     self.linked_list = doubly_linked_list.DoublyLinkedList(array)
예제 #23
0
 def __init__(self):
     self.size = 0
     self.storage = doubly_linked_list.DoublyLinkedList()