def test_search():
    """Test Linked_List length method."""
    from linked_list import Linked_List
    test_list = Linked_List()
    test_list.insert(data[0])
    test_list.insert(data[1])
    assert test_list.search(data[0]) == data[0]
示例#2
0
def test_display():
    a = Linked_List()
    a.display()
    a.insert('a')
    a.insert(1)
    a.display()
    assert a.display_prep() == "(1, 'a')"
def test_insert():
    """Test Linked_List insert method."""
    from linked_list import Linked_List
    from linked_list import Node
    test_list = Linked_List()
    test_list.insert(data[0])
    assert isinstance(test_list.head, Node)
示例#4
0
class Queue:

    def __init__(self):
        self._lst= Linked_List()

    def enqueue(self, datum):
        self._lst.insert(datum)

    def dequeue(self):
        if self._lst.size() > 1:
            trace= self._lst.head
            while trace.pointer.pointer is not None:
                trace= trace.pointer
            result= trace.pointer.datum
            trace.pointer= None
            return result
        elif self._lst.size()==1:
            result= self._lst.head.datum
            self._lst.head= None
            return result
        else:
            raise ValueError("Cannot dequeue an empty queue")

    def size(self):
        return self._lst.size()
def test_list_head_two_nodes():
    list1 = Linked_List()
    list1.insert("Mary")
    list1.insert("Kyle")
    assert list1.size == 2
    assert list1.head.node_name == "Kyle"
    assert list1.head.node_next.node_name == "Mary"
def test_display():
    """Test Linked_List display method."""
    from linked_list import Linked_List
    test_list = Linked_List()
    test_list.insert(data[0])
    test_list.insert(data[1])
    assert test_list.display() == "(123, 456)"
def test_remove():
    """Test Linked_List remove method."""
    from linked_list import Linked_List
    test_list = Linked_List()
    test_list.insert(data[0])
    test_list.insert(data[1])
    test_list.insert(data[2])
    test_list.remove(data[1])
    assert test_list.size() == 2
def test_insert():
    lst= Linked_List()
    lst.insert(7)

    assert lst.head.datum==7

    lst.insert('Sir Sean Connery')

    assert lst.head.datum=='Sir Sean Connery'
示例#9
0
def test_insert():
    lst = Linked_List()
    lst.insert(7)

    assert lst.head.datum == 7

    lst.insert('Sir Sean Connery')

    assert lst.head.datum == 'Sir Sean Connery'
示例#10
0
class Stack:
    def __init__(self):
        self._lst = Linked_List()

    def push(self, datum):
        self._lst.insert(datum)

    def pop(self):
        return self._lst.pop()
示例#11
0
class Stack:

    def __init__(self):
        self._lst= Linked_List()

    def push(self, datum):
        self._lst.insert(datum)

    def pop(self):
        return self._lst.pop()
def test_pop():
    lst= Linked_List()
    lst.insert(7)
    lst.insert('Sir Sean Connery')
    val1= lst.pop()
    val2= lst.pop()

    assert val1=='Sir Sean Connery'
    assert val2==7
    assert lst.head is None
示例#13
0
def test_pop():
    lst = Linked_List()
    lst.insert(7)
    lst.insert('Sir Sean Connery')
    val1 = lst.pop()
    val2 = lst.pop()

    assert val1 == 'Sir Sean Connery'
    assert val2 == 7
    assert lst.head is None
示例#14
0
def test_size():
    s = Linked_List()
    assert s.size() is 0
    for something in range(100):
        s.insert(something)
    assert s.size() is 100
    s.pop()
    assert s.size() is 99
    s.insert(0)
    assert s.size() is 100
    m = List_Node(0)
    s.remove(m)
    assert s.size() is 99
示例#15
0
def create_linked_list(fencers_csv):
    ''' Creates a sorted linked list from a CSV file. '''
    with open(fencers_csv) as csv_file:
        fencer_list = Linked_List()
        reader = csv.reader(csv_file, delimiter='\t')
        for row in reader:
            fencer = {
                'last_name': row[0].split(',')[0],
                'first_name': row[0].split(',')[1],
                'team': row[0].split(',')[2],
                'rank': row[0].split(',')[3]
            }
            fencer_list.insert(fencer)
        return fencer_list
def test_print():
    lst= Linked_List()
    lst.insert('A')
    lst.insert((1, 2, 3))
    lst.insert(42)
    lst.insert('Bob Dole')

    assert str(lst) == "('Bob Dole', 42, (1, 2, 3), 'A')"
示例#17
0
def test_print():
    lst = Linked_List()
    lst.insert('A')
    lst.insert((1, 2, 3))
    lst.insert(42)
    lst.insert('Bob Dole')

    assert str(lst) == "('Bob Dole', 42, (1, 2, 3), 'A')"
示例#18
0
def test_remove():
    a = Linked_List()
    a.insert('val')
    assert a.head
    b = List_Node('val')
    a.remove(b)
    assert not a.head
    for something in range(100):
        a.insert(something)
    c = List_Node(99)
    a.remove(c)
    assert a.head.data is 98
    d = List_Node(0)
    a.remove(d)
    temp = a.head
    while temp.next:
        temp = temp.next
    assert temp.data is 1
示例#19
0
def test_size():
    lst = Linked_List()
    lst.insert(0)
    lst.insert(1)
    lst.insert(45)
    lst.insert('turds')

    assert lst.size() == 4
    assert lst.head.datum == 'turds'
    lst.pop()
    lst.pop()
    assert lst.size() == 2
    assert lst.head.datum == 1
    lst.insert('boobies')
    assert lst.size() == 3
    lst.insert('i am mature')
    assert lst.size() == 4
    lst.pop()
    assert lst.head.datum == 'boobies'
def test_size():
    lst= Linked_List()
    lst.insert(0)
    lst.insert(1)
    lst.insert(45)
    lst.insert('turds')

    assert lst.size()==4
    assert lst.head.datum=='turds'
    lst.pop()
    lst.pop()
    assert lst.size()==2
    assert lst.head.datum==1
    lst.insert('boobies')
    assert lst.size()==3
    lst.insert('i am mature')
    assert lst.size()==4
    lst.pop()
    assert lst.head.datum=='boobies'
def test_search():
    lst= Linked_List()
    lst.insert(7)
    lst.insert(4)
    lst.insert('Sir Sean Connery')
    lst.insert((1, 2, 3))
    lst.insert('Harrison Ford')
    n1= lst.search('Sir Sean Connery')
    n2= lst.search(7)

    assert n1.datum=='Sir Sean Connery'
    assert n2.pointer is None
    assert lst.search('Batman') is None
示例#22
0
def test_search():
    lst = Linked_List()
    lst.insert(7)
    lst.insert(4)
    lst.insert('Sir Sean Connery')
    lst.insert((1, 2, 3))
    lst.insert('Harrison Ford')
    n1 = lst.search('Sir Sean Connery')
    n2 = lst.search(7)

    assert n1.datum == 'Sir Sean Connery'
    assert n2.pointer is None
    assert lst.search('Batman') is None
def test_next():
    list1 = Linked_List()
    list1.insert("John")
    list1.insert("Paul")
    list1.insert("George")
    list1.insert("Ringo")
    assert list1.head.node_next.node_next.node_next.node_name == "John"
示例#24
0
def test_str():
    a = Linked_List()
    print a
    a.insert('When A Café')
    a.insert('b')
    a.insert(2)
    print a
    assert a.display_prep() == "(2, 'b', 'When A Café')"
示例#25
0
class Queue:
    def __init__(self):
        self._lst = Linked_List()

    def enqueue(self, datum):
        self._lst.insert(datum)

    def dequeue(self):
        if self._lst.size() > 1:
            trace = self._lst.head
            while trace.pointer.pointer is not None:
                trace = trace.pointer
            result = trace.pointer.datum
            trace.pointer = None
            return result
        elif self._lst.size() == 1:
            result = self._lst.head.datum
            self._lst.head = None
            return result
        else:
            raise ValueError("Cannot dequeue an empty queue")

    def size(self):
        return self._lst.size()
def test_pop():
    list1 = Linked_List()
    list1.insert("John")
    list1.insert("Paul")
    list1.insert("George")
    list1.insert("Ringo")
    popped = list1.pop()
    assert popped.node_name == "Ringo"
    assert list1.head.node_name == "George"
    assert list1.size == 3
示例#27
0
def test():
    ll = Linked_List(1)
    ll.insert(4)
    ll.insert(16)
    ll.insert(9)
    print(ll.head)
    res = reverse_loop(ll.head)
    print(res)
    print(res.next)
    print(res.next.next)
    print(res.next.next.next)
def test_print():
    list1 = Linked_List()
    list1.insert("John")
    list1.insert("Paul")
    list1.insert("George")
    list1.insert("Ringo")
    assert list1.__str__() == "('Ringo', 'George', 'Paul', 'John')"
    list2 = Linked_List()
    assert list2.__str__() == "()"
    list1.pop()
    assert list1.__str__() == "('George', 'Paul', 'John')"
def test_regular_search():
    list1 = Linked_List()
    list1.insert("John")
    list1.insert("Paul")
    list1.insert("George")
    list1.insert("Ringo")
    searched_node = list1.search("George")
    assert searched_node.node_name == "George"
    searched_node = list1.search("John")
    assert searched_node.node_name == "John"
    searched_node = list1.search("Ralph")
    assert searched_node is None
示例#30
0
def test_remove():
    lst = Linked_List()
    lst.insert(7)
    lst.insert(4)
    lst.insert('Sir Sean Connery')
    lst.insert((1, 2, 3))
    lst.remove(4)
    lst.remove('Sir Sean Connery')

    assert lst.size() == 2
    assert str(lst) == "((1, 2, 3), 7)"
    lst.remove((1, 2, 3))

    assert str(lst) == "(7,)"
def test_remove():
    lst= Linked_List()
    lst.insert(7)
    lst.insert(4)
    lst.insert('Sir Sean Connery')
    lst.insert((1, 2, 3))
    lst.remove(4)
    lst.remove('Sir Sean Connery')

    assert lst.size()==2
    assert str(lst)=="((1, 2, 3), 7)"
    lst.remove((1, 2, 3))

    assert str(lst)=="(7,)"
def test_remove():
    list1 = Linked_List()
    list1.insert("John")
    list1.insert("Paul")
    list1.insert("George")
    list1.insert("Ringo")
    list1.remove("John")
    assert list1.head.node_next.node_next.node_next is None
    list1.remove("George")
    assert list1.size == 2
    assert list1.head.node_next.node_name == "Paul"
    # assert list1.remove("Pete") == "Node not in list"
    with pytest.raises(No_Node_Exception) as excinfo:
        list1.remove("Pete")
    assert excinfo.value.message == '-1'
    list1.remove("Ringo")
class LL_test(unittest.TestCase):
    ''' Linked List unit tests. '''
    LL = None

    def setUp(self):
        ''' Inits LL. '''
        self.LL = Linked_List()

    def test_init(self):
        ''' Tests initialization. '''
        self.assertEqual(self.LL.head, None, 'Initial HEAD should be None.')
        self.assertEqual(self.LL.length, 0, 'Length initializes at 0.')

    def test_insert(self):
        ''' Tests linked_list method: insert(self, data). '''
        node_1 = {
            'first_name': ' Keith',
            'last_name': 'LICHTEN',
            'rank': 'A14',
            'team': 'EBFG '
        }
        self.LL.insert(node_1)
        self.assertDictEqual(
            node_1, self.LL.head.data,
            'Inserting 1 into empty list should create the head.')
        self.assertEqual(self.LL.length, 1,
                         'Length increments when a node is added.')

        node_2 = {
            'first_name': ' Alexandre',
            'last_name': 'RACHTCHININE',
            'rank': 'A15',
            'team': 'NO FEAR'
        }
        self.LL.insert(node_2)
        self.assertDictEqual(
            node_2, self.LL.head.data,
            'Inserting a higher rank node should update the head to point to the new node.'
        )
        self.assertDictEqual(
            node_1, self.LL.head.next_node.data,
            'The previous head should be the new second node. ')
        self.assertEqual(self.LL.length, 2,
                         'Length increments when a node is added.')

        node_3 = {
            'first_name': ' Joseph',
            'last_name': 'HARRINGTON',
            'rank': 'B15',
            'team': 'NO FEAR'
        }
        self.LL.insert(node_3)
        self.assertDictEqual(
            node_3, self.LL.head.next_node.next_node.data,
            'Inserting a low(est) ranking node should insert at the end of the list.'
        )

        node_4 = {
            'first_name': ' Tomas',
            'last_name': 'STRAKA',
            'rank': 'A12',
            'team': ''
        }
        self.LL.insert(node_4)
        self.assertDictEqual(
            node_4, self.LL.head.next_node.next_node.data,
            'Inserting an intermediate rank node should insert at the correct location.'
        )

        node_5 = {
            'first_name': ' Mehmet',
            'last_name': 'TEPEDELENLIOGLU',
            'rank': 'A15',
            'team': 'EBFG '
        }
        self.LL.insert(node_5)
        self.assertDictEqual(
            node_5, self.LL.head.data,
            'Inserting an evenly ranked node should insert at the first apprpriate location.'
        )

    def test_is_new_node_higher_rank(self):
        ''' Tests linked_list method: is_new_node_higher_rank(self, current, new_node). '''
        node_1 = Node({
            'first_name': ' Keith',
            'last_name': 'LICHTEN',
            'rank': 'A14',
            'team': 'EBFG '
        })
        node_2 = Node({
            'first_name': ' Alexandre',
            'last_name': 'RACHTCHININE',
            'rank': 'A15',
            'team': 'NO FEAR'
        })
        node_3 = Node({
            'first_name': ' Mehmet',
            'last_name': 'TEPEDELENLIOGLU',
            'rank': 'A15',
            'team': 'EBFG '
        })

        self.assertFalse(
            self.LL.is_new_node_higher_rank(node_2, node_1),
            'Should return false when evaluating a new node that is lower rank than current.'
        )
        self.assertTrue(
            self.LL.is_new_node_higher_rank(node_1, node_2),
            'Should return true when evaluating a new node that is higher rank than current.'
        )
        self.assertTrue(
            self.LL.is_new_node_higher_rank(node_2, node_3),
            'Should return true when evaluating a new node that is equal rank to current.'
        )

    def test_get_letter_rank(self):
        ''' Tests linked_list method: get_letter_rank(self, node). '''
        node_1 = Node({
            'first_name': ' Keith',
            'last_name': 'LICHTEN',
            'rank': 'A14',
            'team': 'EBFG '
        })
        self.assertEqual(self.LL.get_letter_rank(node_1), 5,
                         'A Node of rank A should return 5.')

        node_2 = Node({
            'first_name': ' Alexandre',
            'last_name': 'RACHTCHININE',
            'rank': 'B15',
            'team': 'NO FEAR'
        })
        self.assertEqual(self.LL.get_letter_rank(node_2), 4,
                         'A Node of rank B should return 4.')

        node_3 = Node({
            'first_name': ' Mehmet',
            'last_name': 'TEPEDELENLIOGLU',
            'rank': 'C15',
            'team': 'EBFG '
        })
        self.assertEqual(self.LL.get_letter_rank(node_3), 3,
                         'A Node of rank C should return 3.')

        node_4 = Node({
            'first_name': ' Alfred',
            'last_name': 'ROEBUCK',
            'rank': 'D13',
            'team': 'NO FEAR'
        })
        self.assertEqual(self.LL.get_letter_rank(node_4), 2,
                         'A Node of rank D should return 2.')

        node_5 = Node({
            'first_name': ' Tracy',
            'last_name': 'COLWELL',
            'rank': 'E15',
            'team': 'EBFG '
        })
        self.assertEqual(self.LL.get_letter_rank(node_5), 1,
                         'A Node of rank E should return 1.')

        node_6 = Node({
            'first_name': ' Tomas',
            'last_name': 'STRAKA',
            'rank': 'U',
            'team': ''
        })
        self.assertEqual(self.LL.get_letter_rank(node_6), 0,
                         'A Node of rank U should return 0.')

    def test_get_year_rank(self):
        ''' Tests linked_list method: get_year_rank(self, node). '''
        node_1 = Node({
            'first_name': ' Keith',
            'last_name': 'LICHTEN',
            'rank': 'A14',
            'team': 'EBFG '
        })
        node_2 = Node({
            'first_name': ' Tomas',
            'last_name': 'STRAKA',
            'rank': 'U',
            'team': ''
        })
        self.assertEqual(
            self.LL.get_year_rank(node_1), str(14),
            'Given a node with a year ranking, should return the year ranking.'
        )
        self.assertEqual(
            self.LL.get_year_rank(node_2), '',
            'Given a node without a year ranking, should return an empty string.'
        )
class TestLinkedList(unittest.TestCase):
    def setUp(self):
        self.linked_list = Linked_List()

    def tearDown(self):
        pass

    def test_count(self):
        count = self.linked_list.count()
        self.assertEqual(count, 0)
        self.linked_list.insert(1)
        count = self.linked_list.count()
        self.assertEqual(count, 1)
        self.linked_list.insert(1)
        count = self.linked_list.count()
        self.assertEqual(count, 2)

    def test_get(self):
        with self.assertRaisesRegex(Exception, 'The list is empty'):
            self.linked_list.get(0)
        self.linked_list.insert(1)
        get = self.linked_list.get(0)
        self.assertEqual(get, 1)
        self.linked_list.insert(1)
        get = self.linked_list.get(1)
        self.assertEqual(get, 1)
        with self.assertRaisesRegex(Exception,
                                    'Your list does not have that index.'):
            self.linked_list.get(3)

    def test_find(self):
        with self.assertRaisesRegex(Exception, 'The list is empty'):
            self.linked_list.find(0)
        self.linked_list.insert(1)
        find = self.linked_list.find(1)
        self.assertEqual(find, 0)
        self.linked_list.insert(2)
        find = self.linked_list.find(2)
        self.assertEqual(find, 1)

    def test_remove_by_index(self):
        with self.assertRaisesRegex(
                Exception, "The list is empty, can't remove any index."):
            self.linked_list.remove_by_index(0)
        self.linked_list.insert(1)
        with self.assertRaisesRegex(
                Exception, 'Your list do not contained that index number'):
            self.linked_list.remove_by_index(5)
        remove_by_index = self.linked_list.remove_by_index(0)
        self.assertEqual(remove_by_index, None)
        self.linked_list.insert(1)
        self.linked_list.insert(1)
        self.linked_list.remove_by_index(0)
        self.assertEqual(self.linked_list.get(0), 1)

    def test_replace_by_index(self):
        with self.assertRaisesRegex(
                Exception, 'The list is empty, can only insert to index 0'):
            self.linked_list.replace_by_index(0, 1)
        self.linked_list.insert(1)
        with self.assertRaisesRegex(
                Exception, 'Your list do not contained that index number'):
            self.linked_list.replace_by_index(5, 2)
        with self.assertRaisesRegex(
                Exception, 'Your list do not contained that index number'):
            self.linked_list.replace_by_index(2, 2)
        self.linked_list.replace_by_index(2, 0)
        self.assertEqual(self.linked_list.find(2), 0)
        self.linked_list.insert(1)
        self.assertEqual(self.linked_list.find(1), 1)

    def test_insert_by_index(self):
        with self.assertRaisesRegex(
                Exception, 'Your list do not contained that index number'):
            self.linked_list.replace_by_index(0, 0)
        with self.assertRaisesRegex(
                Exception, 'The list is empty, can only insert to index 0'):
            self.linked_list.replace_by_index(1, 1)
        self.linked_list.insert(1)
        with self.assertRaisesRegex(
                Exception, 'Your list do not contained that index number'):
            self.linked_list.replace_by_index(0, 4)
        self.linked_list.replace_by_index(2, 0)
        self.assertEqual(self.linked_list.find(2), 0)
        self.linked_list.insert(1)
        self.linked_list.replace_by_index(1, 0)
        self.assertEqual(self.linked_list.find(1), 0)

    def test_insert(self):
        self.linked_list.insert(1)
        self.assertEqual(self.linked_list.get(0), 1)
        self.linked_list.insert(2)
        self.linked_list.insert(3)
        self.assertEqual(self.linked_list.get(2), 3)

    def test_delete_duplicates(self):
        pass
示例#35
0
def test_insert_pop():
    b = Linked_List()
    b.insert(5)
    assert b.head.data is 5
    b.pop()
    assert b.head is None
示例#36
0
def test_search():
    a = Linked_List()
    a.insert('a')
    a.insert(1)
    assert a.search(1).data is 1
    assert a.search('a').data is 'a'
示例#37
0
def test_pop():
    """Test Linked_List pop method."""
    from linked_list import Linked_List
    test_list = Linked_List()
    test_list.insert(data[0])
    assert test_list.pop().data == data[0]
# Time Comp: O(n2)
# Space Comp: O(1)
def remove_dupes_1(ll):
    curr = ll.head
    while curr:
        runner = ll.head
        prev = ll.head
        while runner:
            if curr.data is runner.data:
                prev.next_node = runner.next_node
            else:
                prev = prev.next_node
            runner = runner.next_node
        curr = curr.next_node


L1 = Linked_List()
L1.insert(5)
L1.insert(3)
L1.insert(5)
L1.insert(5)
L1.insert(10)
L1.insert(5)
L1.insert(10)

L1.output()

print("Removing all duplicates...")
# remove_dupes(L1)
# remove_dupes_1(L1)
L1.output()
def test_list_head_when_none():
    list1 = Linked_List()
    list1.insert("Mary")
    assert list1.head.node_name == "Mary"
    assert list1.size == 1
    assert list1.head.node_next is None