示例#1
0
    def test_exercise_4(self):
        linked_list_a = LinkedList.from_array([1, 2, 3])
        linked_list_b = LinkedList.from_array([3, 2, 1])
        linked_list_c = LinkedList.from_array([0, 0, 7, 1])

        assert (linked_list_a + linked_list_b).to_array() == [4, 4, 4]

        assert (linked_list_a + linked_list_c).to_array() == [1, 2, 0, 2]
示例#2
0
    def test_exercise_4(self):
        linked_list_a = LinkedList.from_array([1, 2, 3])
        linked_list_b = LinkedList.from_array([3, 2, 1])
        linked_list_c = LinkedList.from_array([0, 0, 7, 1])

        assert (linked_list_a + linked_list_b).to_array() == [4, 4, 4]

        assert (linked_list_a + linked_list_c).to_array() == [1, 2, 0, 2]
示例#3
0
    def test_exercise_1(self):
        assert LinkedList.from_array([]).remove_duplicates().to_array() == []

        assert LinkedList.from_array([1, 1, 1, 1, 1, 1
                                      ]).remove_duplicates().to_array() == [1]

        assert LinkedList.from_array(
            [1, 3, 4, 6, 2, 3,
             1]).remove_duplicates().to_array() == [1, 3, 4, 6, 2]
示例#4
0
    def test_exercise_5(self):
        linked_list = LinkedList.from_array([1, 2, 3, 4, 5])

        assert linked_list.find_loop() is None

        # Insert a circular dependency 5 -> 2
        linked_list.get_tail_node().set_next_node(linked_list.head.get_next_node())

        assert linked_list.find_loop().value == 2

        empty_linked_list = LinkedList.from_array([])

        assert_raises(IndexError, empty_linked_list.get_tail_node)
示例#5
0
    def test_exercise_5(self):
        linked_list = LinkedList.from_array([1, 2, 3, 4, 5])

        assert linked_list.find_loop() is None

        # Insert a circular dependency 5 -> 2
        linked_list.get_tail_node().set_next_node(
            linked_list.head.get_next_node())

        assert linked_list.find_loop().value == 2

        empty_linked_list = LinkedList.from_array([])

        assert_raises(IndexError, empty_linked_list.get_tail_node)
示例#6
0
    def test_exercise_2(self):
        assert list(
            LinkedList.from_array(
                [1, 2, 3, 4, 5, 6,
                 7]).get_nth_to_last(0)) == [1, 2, 3, 4, 5, 6, 7]

        assert list(
            LinkedList.from_array([1, 2, 3, 4, 5, 6,
                                   7]).get_nth_to_last(5)) == [6, 7]

        assert_raises(
            IndexError, lambda x: list(
                LinkedList.from_array([1, 2, 3, 4, 5, 6, 7]).get_nth_to_last(x)
            ), 7)
示例#7
0
    def test_linked_list_get(self):
        ll = LinkedList.from_array([1, 2, 3, 4])

        assert ll.get(3).value == 4

        assert ll.get(0).value == 1

        assert len(ll) == 4

        assert_raises(IndexError, ll.get, 4)
示例#8
0
    def test_linked_list_insert(self):
        ll = LinkedList.from_array([1, 2, 3])

        ll.insert(0, 4)

        assert ll.to_array() == [4, 1, 2, 3]
        assert len(ll) == 4

        ll.insert(4, 10)

        assert ll.to_array() == [4, 1, 2, 3, 10]
        assert len(ll) == 5
示例#9
0
    def test_linked_list_remove(self):
        ll = LinkedList.from_array([2, 3, 4, 5, 6])

        assert len(ll) == 5

        ll.remove(0)

        assert ll.to_array() == [3, 4, 5, 6]
        assert len(ll) == 4

        ll.remove(3)

        assert ll.to_array() == [3, 4, 5]
        assert len(ll) == 3

        assert_raises(IndexError, ll.remove, 3)
示例#10
0
    def test_exercise_1(self):
        assert LinkedList.from_array([]).remove_duplicates().to_array() == []

        assert LinkedList.from_array([1, 1, 1, 1, 1, 1]).remove_duplicates().to_array() == [1]

        assert LinkedList.from_array([1, 3, 4, 6, 2, 3, 1]).remove_duplicates().to_array() == [1, 3, 4, 6, 2]
示例#11
0
    def test_exercise_2(self):
        assert list(LinkedList.from_array([1, 2, 3, 4, 5, 6, 7]).get_nth_to_last(0)) == [1, 2, 3, 4, 5, 6, 7]

        assert list(LinkedList.from_array([1, 2, 3, 4, 5, 6, 7]).get_nth_to_last(5)) == [6, 7]

        assert_raises(IndexError, lambda x: list(LinkedList.from_array([1, 2, 3, 4, 5, 6, 7]).get_nth_to_last(x)), 7)
示例#12
0
    def test_linked_list_reverse(self):
        ll = LinkedList.from_array([1, 2, 3])

        ll.reverse()

        assert ll.to_array() == [3, 2, 1]
示例#13
0
    def test_linked_list_iter(self):
        ll = LinkedList.from_array([1, 2, 3])

        assert list(ll) == [1, 2, 3]