def test_linked_prepend_2():
    l = DoubleLinkedList()
    l.prepend(5)
    l.prepend(7)
    assert l.front.value == 7
    assert l.front.next.value == 5
    assert l.end.value == 5
    assert_contains(l, [7, 5])
def test_insert_after():
    l = DoubleLinkedList()
    l.append(5)
    l.append(7)
    l.append(9)
    l.insert_after(l.front, 13)
    assert l.front.next.value == 13
    assert_contains(l, [5, 13, 7, 9])
def test_remove_3():
    l = DoubleLinkedList()
    l.append(5)
    l.append(7)
    l.append(9)
    l.remove(l.front)
    assert l.front.value == 7
    assert l.end.value == 9
    assert_contains(l, [7, 9])
def test_remove_2():
    l = DoubleLinkedList()
    l.append(5)
    l.append(7)
    l.append(9)
    l.remove(l.end)
    assert l.front.value == 5
    assert l.end.value == 7
    assert_contains(l, [5, 7])
def test_remove():
    l = DoubleLinkedList()
    l.append(5)
    l.append(7)
    l.append(9)
    l.remove(l.front.next)
    assert l.front.value == 5
    assert l.front.next.value == 9
    assert_contains(l, [5, 9])
def test_remove_4():
    l = DoubleLinkedList()
    l.append(5)
    l.remove(l.front)
    assert l.front is None
    assert l.end is None
    assert_contains(l, [])
Esempio n. 7
0
class Queue:
    def __init__(self):
        self.__list = DoubleLinkedList()

    def enqueue(self, val):
        self.__list.add(val)

    def dequeue(self):
        val = self.__list.front()
        self.__list.remove_first()
        return val

    def is_empty(self):
        return self.__list.size == 0

    def front(self):
        return self.__list.front()

    def __len__(self):
        return self.__list.size


# my_stack=Stack()
# my_stack.push(1)
# my_stack.push(2)
# my_stack.push(3)
# my_stack.push(5)
# print(my_stack.peek())
# print(len(my_stack))
# print(my_stack.pop())
# print(len(my_stack))
# print(my_stack.peek())
def test_remove_6():
    l = DoubleLinkedList()
    l.append(5)
    l.append(7)
    l.remove(l.end)
    assert l.front.value == 5
    assert l.end is not None
    assert_contains(l, [5])
def is_palindrome(l):

    # Palindrome requires to be odd
    if len(l) % 2 == 0:
        print('Imposible - Palidromes require to have an odd lenght')
        return False

    ll = DoubleLinkedList(list(l))
    runner1 = ll.head.data
    runner2 = ll.tail.data
    print('LIST:')
    print(ll)

    for _ in range(len(ll) // 2):

        #        r1, r2 = runner1.data, runner2.data
        #        print('R1: {},  R2: {}'.format(r1, r2))  # -> this prints the two integers (1) would return True
        if runner1.data != runner2.data:  # this is comparing two <linked_list.Node object at 0x1244aab70> and returns False
            return False

        else:
            runner1 = runner1.next
            runner2 = runner2.prev
    return True
Esempio n. 10
0
class Stack:
    def __init__(self):
        self.__list=DoubleLinkedList()

    def push(self,val):
        self.__list.add(val)

    def pop(self):
        val = self.__list.back()
        self.__list.remove_last()
        return val

    def is_empty(self):
        return self.__list.size==0

    def peek(self):
        return self.__list.back()

    def __len__(self):
        return self.__list.size
 def __init__(self, max_size):
     self.max_size = max_size
     self.cache = {}
     self.accesses = DoubleLinkedList()
class LRUCache:
    class Node:
        def __init__(self, value, access_ref):
            self.value = value
            self.access_ref = access_ref

    def __init__(self, max_size):
        self.max_size = max_size
        self.cache = {}
        self.accesses = DoubleLinkedList()

    def set(self, key, value):
        self._evict()
        if key in self.cache:
            list_ref = self.cache[key].access_ref
            self.accesses.remove(list_ref)
            self.accesses._append(list_ref)
        else:
            list_ref = self.accesses.append(key)

        self.cache[key] = LRUCache.Node(value, list_ref)

    def get(self, key):
        if key not in self.cache:
            raise ValueError('{} not found in cache'.format(key))

        list_ref = self.cache[key].access_ref
        self.accesses.remove(list_ref)
        self.accesses._append(list_ref)
        return self.cache[key].value

    def _evict(self):
        if self.max_size == len(self.cache):
            node = self.accesses.front
            self.accesses.remove(node)
            del self.cache[node.value]
def test_insert_after_2():
    l = DoubleLinkedList()
    l.append(5)
    l.insert_after(l.front, 13)
    assert l.end.value == 13
    assert_contains(l, [5, 13])
def test_linked_prepend():
    l = DoubleLinkedList()
    l.prepend(5)
    assert l.front.value == 5
    assert l.end.value == 5
    assert_contains(l, [5])
def test_linked():
    l = DoubleLinkedList()
    l.append(5)
    assert l.front.value == 5
    assert l.end.value == 5
Esempio n. 16
0
 def __init__(self):
     self.__list=DoubleLinkedList()