当前位置: 首页>>代码示例>>Python>>正文


Python LinkedList._get_node_at_index方法代码示例

本文整理汇总了Python中data_structures.linked_list.LinkedList._get_node_at_index方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList._get_node_at_index方法的具体用法?Python LinkedList._get_node_at_index怎么用?Python LinkedList._get_node_at_index使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在data_structures.linked_list.LinkedList的用法示例。


在下文中一共展示了LinkedList._get_node_at_index方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestLinkedList

# 需要导入模块: from data_structures.linked_list import LinkedList [as 别名]
# 或者: from data_structures.linked_list.LinkedList import _get_node_at_index [as 别名]

#.........这里部分代码省略.........
        self.ll.insert_at_index(3, 'Data3.5')
        self.assertEqual(self.ll[3], 'Data3.5', 'Insert at index should set the data at the index')
        self.assertEqual(self.ll[2], 'Data3', 'Insert at index should not change the data at the index before it')
        self.assertEqual(self.ll[4], 'Data4', 'Insert at index should move the other data up an index')
        with self.assertRaises(IndexError, msg='Insert at out of bounds index should raise IndexError'):
            self.ll.insert_at_index(len(self.ll) + 1, 'Data7')

    def test_positive_set(self):
        with self.assertRaises(IndexError, msg='Set at positive out of bounds should raise IndexError'):
            self.ll[5] = 'Data'
        self.ll[0] = 'Data1'
        self.assertEqual(self.ll['head'], 'Data1', 'Set at 0 should update head pointer in linked list with no items')
        self.assertEqual(self.ll['tail'], 'Data1', 'Set at 0 should update tail pointer in linked list with no items')
        self.ll[0] = 'Data2'
        self.assertEqual(self.ll['head'], 'Data2', 'Set at 0 should update head pointer in linked list with one item')
        self.assertEqual(self.ll['tail'], 'Data2', 'Set at 0 should update tail pointer in linked list with one item')
        self.ll[1] = 'Data3'
        self.assertEqual(self.ll['head'], 'Data2', 'Set at 1 should not update head pointer in linked list with one item')
        self.assertEqual(self.ll['tail'], 'Data3', 'Set at 1 should update tail pointer in linked list with one item')

    def test_negative_set(self):
        with self.assertRaises(IndexError, msg='Set at negative out of bounds should raise IndexError'):
            self.ll.set_at_index(-2, 'Data')
        self.ll.set_at_index(-1, 'Data1')
        self.assertEqual(self.ll['head'], 'Data1', 'Set at -1 should update head pointer in linked list with no items')
        self.assertEqual(self.ll['tail'], 'Data1', 'Set at -1 should update tail pointer in linked list with no items')
        self.ll.set_at_index(-1, 'Data2')
        self.assertEqual(self.ll['head'], 'Data2', 'Set at -1 should update head pointer in linked list with one item')
        self.assertEqual(self.ll['tail'], 'Data2', 'Set at -1 should update tail pointer in linked list with one item')
        self.ll.set_at_index(-2, 'Data3')
        self.assertEqual(self.ll['head'], 'Data3', 'Set at -2 should update head pointer in linked list with one item')
        self.assertEqual(self.ll['tail'], 'Data2', 'Set at -2 should not update tail pointer in linked list with one item')
        self.ll.set_at_index(-3, 'Data4')
        self.assertEqual(self.ll['head'], 'Data3', 'Set at -3 should update head pointer in linked list with multiple items')
        self.assertEqual(self.ll['tail'], 'Data2', 'Set at -3 should not update tail pointer in linked list with multiple items')

    def test_remove_items(self):
        with self.assertRaises(ValueError, msg='Remove functions should raise ValueError on empty linked list'):
            self.ll.remove_item(None)
        with self.assertRaises(ValueError, msg='Remove functions should raise ValueError on empty linked list'):
            self.ll.remove_head()
        with self.assertRaises(ValueError, msg='Remove functions should raise ValueError on empty linked list'):
            self.ll.remove_tail()
        self.ll = LinkedList(1, 2, 3, 4, 5, 3)
        self.ll.remove_item(2)
        self.assertFalse(2 in self.ll)
        self.ll.remove_item(3)
        self.assertTrue(3 in self.ll)
        self.ll.remove_head()
        self.assertEqual(self.ll['head'], 4)
        with self.assertRaises(ValueError):
            self.ll.remove_item('None')
        with self.assertRaises(ValueError):
            self.ll.remove_item(None)

    def test_list_comprehension(self):
        self.ll = LinkedList(1, 2, 3, 4, 5, 6)
        self.assertListEqual([item for item in self.ll], [1, 2, 3, 4, 5, 6])
        self.assertListEqual(list(reversed([item for item in self.ll])), [6, 5, 4, 3, 2, 1])

    def test_size(self):
        self.assertEqual(len(self.ll), 0)
        self.assertEqual(len(self.ll), self.ll.calculate_size())
        self.ll.insert_at_head(3)
        self.assertEqual(len(self.ll), 1)
        self.assertEqual(len(self.ll), self.ll.calculate_size())
        self.ll.insert_at_tail(4)
        self.assertEqual(len(self.ll), 2)
        self.assertEqual(len(self.ll), self.ll.calculate_size())

    def test_text_representation(self):
        self.assertEqual(repr(self.ll), 'LinkedList()')
        self.ll = LinkedList(1)
        self.assertEqual(repr(self.ll), 'LinkedList(1)')
        self.ll = LinkedList([1, 2, 3, 4])
        self.assertEqual(repr(self.ll), 'LinkedList([1, 2, 3, 4])')
        self.ll = LinkedList(1, 2, 3, 4)
        self.assertEqual(repr(self.ll), 'LinkedList(1, 2, 3, 4)')

    def test_node_removal(self):
        self.ll = LinkedList(4, 7, 9, 11)
        check_node = self.ll._get_node_at_index(2)
        with self.assertRaises(ValueError):
            self.ll._remove_node(self.ll.tail)
        self.ll._remove_node(self.ll.head)
        self.assertEqual(self.ll, LinkedList(7, 9, 11))
        self.ll._remove_node(check_node)
        self.assertEqual(self.ll, LinkedList(7, 11))

    def test_merge(self):
        self.ll = LinkedList(4, 7, 9, 11)
        self.assertEqual(self.ll.merge(LinkedList(2, 5, 6, 12, 15, 16)),
                         LinkedList(2, 4, 5, 6, 7, 9, 11, 12, 15, 16),
                         'Merge linked lists should return a sorted linked list')
        self.assertEqual(self.ll.merge(LinkedList(5, 12, 15, 16)),
                         LinkedList(4, 5, 7, 9, 11, 12, 15, 16),
                         'Merge linked lists should return a sorted linked list')
        self.assertEqual(self.ll.merge(LinkedList()),
                         LinkedList(4, 7, 9, 11),
                         'Merge linked lists should return a sorted linked list')
开发者ID:ignat980,项目名称:CS-Theory,代码行数:104,代码来源:test_linked_list.py


注:本文中的data_structures.linked_list.LinkedList._get_node_at_index方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。