def test_print_empty_list(capsys):
    linked_list = LinkedList()
    linked_list.print()

    expected = "NULL\n"
    output = capsys.readouterr().out
    assert output == expected
def test_add_at_head(ll):
    ll.addAtHead(5)
    assert ll.head.data == 5

    empty_ll = LinkedList()
    empty_ll.addAtHead(1)
    assert empty_ll.head.data == 1
def test_add_at_tail(ll):
    ll.addAtTail(8)
    assert ll.get(5) == 8

    empty_ll = LinkedList()
    empty_ll.addAtTail(1)
    assert empty_ll.head.data == 1
Ejemplo n.º 4
0
def test_linked_list_push_new_to_old():
    """Test linked list push data new head should point to the old head."""
    from src.linked_list import LinkedList
    linked_list = LinkedList(1)
    old = linked_list.head
    linked_list.push(2)
    assert linked_list.head.next_item == old
Ejemplo n.º 5
0
def test_lists():
    """Fixture for linked list tests."""
    from src.linked_list import LinkedList
    empty = LinkedList()
    one = LinkedList(5)
    multi = LinkedList([1, 2, 3, 4, 5])
    return empty, one, multi
def test_print_list_with_one_element(capsys):
    linked_list_node = LinkedListNode(1)
    linked_list = LinkedList(linked_list_node)
    linked_list.print()

    expected = "1 -> NULL\n"
    output = capsys.readouterr().out
    assert output == expected
Ejemplo n.º 7
0
 def test_reverse(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.append(5)
     llist.reverse()
     self.assertEqual(str(llist), '54321')
Ejemplo n.º 8
0
 def test_last_to_first(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.append(5)
     llist.last_to_first()
     self.assertEqual(str(llist), '51234')
Ejemplo n.º 9
0
def test_merge():
    l = [4, 7, 2, 8, 4, 6, 9, 5, 5, 7, 3, 2, 8]
    l_sorted = sorted(l)
    l_left = LinkedList.from_list(sorted(l[:5]))
    l_right = LinkedList.from_list(sorted(l[5:]))
    ll = LinkedList.merge(l_left, l_right)
    assert len(ll) == len(l)
    for i, j in zip(ll, l_sorted):
        assert i == j
Ejemplo n.º 10
0
 def test_clear(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.append(5)
     llist.clear()
     self.assertIsNone(llist.head)
Ejemplo n.º 11
0
 def test_pairwise_swap(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.append(5)
     llist.append(6)
     llist.pairwise_swap()
     self.assertEqual(str(llist), '214365')
Ejemplo n.º 12
0
 def test_rotate(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.append(5)
     llist.append(6)
     llist.rotate(3)
     self.assertEqual(str(llist), '456123')
Ejemplo n.º 13
0
    def populate_list(llist: LinkedList):
        nodes = (
            Node(12),
            Node(34),
            Node(56),
        )
        for node in nodes:
            llist.add_in_tail(node)

        return nodes
Ejemplo n.º 14
0
def test_linked_list_remove_phase_three():
    """Test proper remove functionality."""
    from src.linked_list import LinkedList
    new_list = LinkedList([1, 2, 3, 4])
    new_list.remove(1)
    new_list.remove(4)
    new_list.push(5)
    new_list.remove(3)
    assert (new_list.search(3),
            new_list.search(5).data) == (None, new_list.head.data)
class LLQueue:
    def __init__(self):
        self.storage = LinkedList()

    # adds a value to the back of the queue
    def enqueue(self, value):
        self.storage.add_to_tail(value)  # O(1)

    # removes a value from the front of the queue
    def dequeue(self):
        return self.storage.remove_head()  # O(1)
Ejemplo n.º 16
0
def merge(lists):
    r = LinkedList()
    key_f = lambda a: -a.head.val
    lists = [l for l in lists if l.head]
    lists = sorted(lists, key=key_f)
    while lists:
        current_list = lists.pop()
        r.push_tail(current_list.pop_head())
        if current_list:
            insort(lists, current_list, key=key_f)
    return r
def test_find_in_list():
    node_1 = LinkedListNode(1)
    node_2 = LinkedListNode(2)
    node_3 = LinkedListNode(3)
    linked_list = LinkedList(node_1)
    linked_list.head.next = node_2
    node_2.next = node_3

    expected = True
    output = linked_list.find(3)
    assert output == expected
Ejemplo n.º 18
0
 def test_occurrences(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(1)
     llist.append(4)
     llist.append(1)
     self.assertEqual(llist.occurrences(1), 3)
Ejemplo n.º 19
0
 def test_middle(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.append(5)
     self.assertEqual(llist.get_middle(), 3)
Ejemplo n.º 20
0
 def test_print_reverse(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.append(5)
     self.assertEqual(llist.print_reverse(), '54321')
Ejemplo n.º 21
0
 def test_reverse_index(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.append(5)
     self.assertEqual(llist.reverse_index(4), 1)
Ejemplo n.º 22
0
 def test_palindrome(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(2)
     llist.append(1)
     self.assertTrue(llist.is_palindrome())
def test_print_list_with_multiple_elements(capsys):
    node_1 = LinkedListNode(1)
    node_2 = LinkedListNode(2)
    node_3 = LinkedListNode(3)
    linked_list = LinkedList(node_1)
    linked_list.head.next = node_2
    node_2.next = node_3
    linked_list.print()

    expected = "1 -> 2 -> 3 -> NULL\n"
    output = capsys.readouterr().out
    assert output == expected
Ejemplo n.º 24
0
    def __init__(self, name, age, location=None):
        if not isinstance(age, int):
            raise Exception("Age can be only a integer")

        self.name = name
        self.age = age

        self.status = GangsterStatus.FREE
        self.location = location or GangsterLocation.UNKNOWN

        self.__boss = None
        self.__subordinates = LinkedList()
Ejemplo n.º 25
0
 def test_detect_loop(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.append(5)
     llist.head.next.next.next.next.next = llist.head
     self.assertTrue(llist.detect_loop())
Ejemplo n.º 26
0
class Stack(object):
    """Stack object for creating a stack list."""
    def __init__(self, data=None):
        """Initialize stack class."""
        self._container = LinkedList(data)

    def push(self, value):
        """Add a value to the stack."""
        return self._container.push(value)

    def pop(self):
        """Remove a value from the stack and returns that value."""
        return self._container.pop()
Ejemplo n.º 27
0
 def test_alternate_split(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.append(5)
     llist.append(6)
     a, b = llist.alternate_split()
     self.assertEqual(str(a), '135')
     self.assertEqual(str(b), '246')
Ejemplo n.º 28
0
 def test_delete_at(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.delete_at(2)
     self.assertEqual(str(llist), '124')
Ejemplo n.º 29
0
 def test_Swap(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.swap(2,3)
     self.assertEqual(str(llist), '1324')
Ejemplo n.º 30
0
 def test_insert_after(self):
     llist = LinkedList()
     llist.append(1)
     llist.append(2)
     llist.append(3)
     llist.append(4)
     llist.insert_after(2, 2.5)
     self.assertEqual(str(llist), '122.534')
Ejemplo n.º 31
0
class Gangster:
    def __init__(self, name, age, location=None):
        if not isinstance(age, int):
            raise Exception("Age can be only a integer")

        self.name = name
        self.age = age

        self.status = GangsterStatus.FREE
        self.location = location or GangsterLocation.UNKNOWN

        self.__boss = None
        self.__subordinates = LinkedList()


    def subordinates_count(self):
        return len(self.__subordinates)

    def add_subordinate(self, gangster):
        if not isinstance(gangster, Gangster):
            raise Exception("You can add only Gangsters as subordinates")

        gangster.set_boss(self)
        return self.__subordinates.add_first(gangster)

    def delete_subordinate(self, gangster):
        if not isinstance(gangster, Gangster):
            raise Exception("You can delete only Gangsters subordinates")

        return self.__subordinates.remove(gangster)

    def subordinates(self):
        return self.__subordinates.get_elements()

    def set_boss(self, gangster):
        if not isinstance(gangster, Gangster) and gangster is not None:
            raise Exception("You can add only Gangsters as bosses")

        self.__boss = gangster

        return True

    def get_boss(self):
        return self.__boss

    def has_boss(self):
        return self.__boss is not None
Ejemplo n.º 32
0
def test_LinkedList_search(a):
    from src.linked_list import LinkedList
    test_values = LinkedList(a)
    assert test_values.search(test_values.head.data) == test_values.head
Ejemplo n.º 33
0
def test_LinkedList_size(a):
    from src.linked_list import LinkedList
    test_values = LinkedList(a)
    assert test_values.size() == len(a)
Ejemplo n.º 34
0
def test_LinkedList_4(a, b):
    from src.linked_list import LinkedList
    test_values = LinkedList(a)
    assert test_values.display() == b
Ejemplo n.º 35
0
def test_LinkedList_insert(a):
    from src.linked_list import LinkedList
    this_list = LinkedList([1])
    this_list.insert(a)
    assert this_list.head.data == a
Ejemplo n.º 36
0
def test_LinkedList_remove_2(a):
    from src.linked_list import LinkedList
    this_list = LinkedList(a)
    this_list.remove(this_list.head)
    assert this_list.size() == 0