Ejemplo n.º 1
0
class Buffer:
    def __init__(self, name: str):
        self.name = name
        self.file = None
        self.buffer = DoublyLinkedList(2)
        
        self.cursorX = 0
        self.cursorY = 0
    
    def newLine(self, contents: str = ""):
        nl = DoublyLinkedList(2)
        for char in contents:
            nl.append(char)
        self.buffer.append(nl)

    def getLine(self, lineno: int):
        line = ""
        for char in self.buffer.getIndex(lineno).Traverse():
            line += char
        return line
    
    def getBuffer(self):
        ret = []
        for lineno in range(self.buffer.totalCount):
            ret.append(self.getLine(lineno))
        return ret
Ejemplo n.º 2
0
    def test_length(self):

        dll = DoublyLinkedList()
        assert dll.length() == 0
        dll.append('A')
        assert dll.length() == 1
        dll.append('B')
        assert dll.length() == 2
Ejemplo n.º 3
0
    def test_items(self):

        dll = DoublyLinkedList()
        assert dll.items() == []
        dll.append('A')
        assert dll.items() == ['A']
        dll.append('B')
        assert dll.items() == ['A', 'B']
Ejemplo n.º 4
0
def partition(ll, div_node):
    temp_ll = DoublyLinkedList()

    if ll.head is not None:
        curr_node = ll.head
    else:
        return ll

    while curr_node.value is not None:

        curr_node_next = curr_node.next
        curr_node_prev = curr_node.prev

        if curr_node.value >= div_node.value:
            temp_ll.append(DoublyNode(value=curr_node.value))

            if curr_node_prev:
                curr_node_prev.next = curr_node_next
                curr_node_next.prev = curr_node_prev

        curr_node = curr_node_next

    ll.append(temp_ll.head)
    return ll
Ejemplo n.º 5
0
    def test_append(self):

        dll = DoublyLinkedList()
        dll.append('A')
        assert dll.head.data == 'A'
        assert dll.tail.data == 'A'
        assert dll.size == 1

        dll.append('B')
        assert dll.head.data == 'A'
        assert dll.tail.data == 'B'
        assert dll.size == 2

        dll.append('C')
        assert dll.head.data == 'A'
        assert dll.tail.data == 'C'
        assert dll.size == 3
Ejemplo n.º 6
0
class TestDLLMethods(unittest.TestCase):
    def setUp(self):
        self.ll = DoublyLinkedList(2)
    
    def getData(self) -> list:
        return [x.getData() for x in self.ll.Traverse()]
    
    def getSeeks(self) -> list:
        return [x.getData() for x in self.ll.seeks]

    def testGet(self):
        # Bit of a chicken-and-egg:
        # Relies on append functioning as intended, yet seeing that append
        # functions as intended requires the get parametres to work properly.
        self.ll.append("node1")
        self.ll.append("node2")
        self.ll.append("node3")
        self.assertEqual(self.getData(), ["node1", "node2", "node3"])
        self.assertEqual([x.data for x in self.ll.revTraverse()], ["node3", "node2", "node1"])
        self.assertEqual(self.ll.getIndex().data, "node3")
        self.assertEqual(self.ll.getIndex(1).data, "node2")
        

    def test_append(self):
        # Case 1: Append to an empty list with index supplied
        # Shows that isEmpty is functional, therefore no need to test other indicies.
        self.ll.append("node1", -1)
        self.assertEqual(self.getData(), ["node1"])
        self.assertEqual(self.getSeeks(), ["node1"])
        
        # Case 2: Append to the start of the list
        self.ll.append("node2", 0)
        self.assertEqual(self.getData(), ["node2", "node1"])
        self.assertEqual(self.getSeeks(), ["node2"])
        
        # Case 3: Append somewhere in between
        # Also tests if a new seek is added when conditions are satisfied.
        self.ll.append("node3", 1)
        self.assertEqual(self.getData(), ["node2", "node3", "node1"])
        self.assertEqual(self.getSeeks(), ["node2", "node1"])
        
        # Case 4: Append to the end
        # Also tests if the seek system handles the addition correctly.
        self.ll.append("node4", -1)
        self.assertEqual(self.getData(), ["node2", "node3", "node1", "node4"])
        self.assertEqual(self.getSeeks(), ["node2", "node1"])

        # Cases 5, 6: Appends to invalid indicies
        # Makes sure that nothing happens when an invalid index is passed.
        self.ll.append("node5", -2)
        self.assertEqual(self.getData(), ["node2", "node3", "node1", "node4"])
        self.assertEqual(self.getSeeks(), ["node2", "node1"])

        self.ll.append("node6", 100)
        self.assertEqual(self.getData(), ["node2", "node3", "node1", "node4"])
        self.assertEqual(self.getSeeks(), ["node2", "node1"])
    
    def test_delete(self):
        # Setup: creates the same list as in the prior test for convenience
        self.ll.append("node1", -1)
        self.ll.append("node2", 0)
        self.ll.append("node3", 1)
        self.ll.append("node4", -1)
        
        # Case 1, 2: Delete from invalid indicies
        # Makes sure that nothing happens when an invalid index is passed
        el = self.ll.delete(-2)
        self.assertEqual(self.getData(), ["node2", "node3", "node1", "node4"])
        self.assertEqual(self.getSeeks(), ["node2", "node1"])
        self.assertIsNone(el)

        el = self.ll.delete(100)
        self.assertEqual(self.getData(), ["node2", "node3", "node1", "node4"])
        self.assertEqual(self.getSeeks(), ["node2", "node1"])
        self.assertIsNone(el)

        # Case 3: Delete from end
        # Also tests if seek system handles the removal correctly.
        el = self.ll.delete(-1)
        self.assertEqual(self.getData(), ["node2", "node3", "node1"])
        self.assertEqual(self.getSeeks(), ["node2", "node1"])
        self.assertEqual(el.getData(), "node4")
        
        # Case 4: Delete from somewhere in between
        # Also tests if a seek is removed when conditions are satisfied
        el = self.ll.delete(1)
        self.assertEqual(self.getData(), ["node2", "node1"])
        self.assertEqual(self.getSeeks(), ["node2"])
        self.assertEqual(el.getData(),"node3")

        # Case 5: Delete from the start of the list.
        el = self.ll.delete(0)
        self.assertEqual(self.getData(), ["node1"])
        self.assertEqual(self.getSeeks(), ["node1"])
        self.assertEqual(el.getData(), "node2")
        
        # Case 6: Delete final element
        el = self.ll.delete(-1)
        self.assertEqual(self.getData(), [])
        self.assertEqual(self.getSeeks(), [])
        self.assertEqual(el.getData(), "node1")
import sys
sys.path.append('./linkedlist')
from linkedlist import DoublyLinkedList, DoublyNode
from get_kth_to_last import get_ll_size


def delete_mid_node(node):
    node.prev.next = node.next
    node.next.prev = node.prev


if __name__ == '__main__':  # tests

    ll = DoublyLinkedList()
    dnode1 = DoublyNode(value=1)
    dnode2 = DoublyNode(value=2)
    dnode3 = DoublyNode(value=3)
    ll.append(dnode1)
    ll.append(dnode2)
    ll.append(dnode3)
    assert get_ll_size(ll) == 3
    assert ll.head.next == dnode2

    delete_mid_node(dnode2, )
    assert get_ll_size(ll) == 2
    assert ll.head.next != dnode2
Ejemplo n.º 8
0
    curr_backwards = ll.tail
    is_palindrome = True

    while curr_forwards and curr_backwards and curr_backwards != curr_forwards and is_palindrome:
        if curr_forwards.value != curr_backwards.value:
            is_palindrome = False
        curr_forwards = curr_forwards.next
        curr_backwards = curr_backwards.prev

    return is_palindrome


if __name__ == '__main__':  # tests
    palidrome = [1, 2, 3, 4, 5, 4, 3, 2, 1]
    palidrome_ll = DoublyLinkedList()
    for p in palidrome:
        palidrome_ll.append(DoublyNode(value=p))
    assert is_palindrome(palidrome_ll) == True

    palidrome = [1, 2, 3, 4, 4, 3, 2, 1]
    palidrome_ll = DoublyLinkedList()
    for p in palidrome:
        palidrome_ll.append(DoublyNode(value=p))
    assert is_palindrome(palidrome_ll) == True

    not_palidrome = [1, 2, 3, 4, 5, 3, 2, 1]
    not_palidrome_ll = DoublyLinkedList()
    for p in not_palidrome:
        not_palidrome_ll.append(DoublyNode(value=p))
    assert is_palindrome(not_palidrome_ll) == False
Ejemplo n.º 9
0
        curr_node = curr_node_next

    ll.append(temp_ll.head)
    return ll


if __name__ == '__main__':  # tests

    ll = DoublyLinkedList()
    origin_node_values = [3, 5, 8, 5, 10, 2, 1]
    partition_value = 5

    for node_value in origin_node_values:
        node = DoublyNode(value=node_value)
        ll.append(node)

    after_partition_value = False
    after_partition = partition(ll, DoublyNode(value=partition_value))

    curr_node = after_partition.head
    while curr_node.value:
        if node.value >= partition_value:
            after_partition_value = True
        if after_partition_value:
            assert node.value >= partition_value
        else:
            assert node.value < partition

        curr_node = curr_node.next
Ejemplo n.º 10
0
 def newLine(self, contents: str = ""):
     nl = DoublyLinkedList(2)
     for char in contents:
         nl.append(char)
     self.buffer.append(nl)