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]
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]
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)
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)
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)
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)
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
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)
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]
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)
def test_linked_list_reverse(self): ll = LinkedList.from_array([1, 2, 3]) ll.reverse() assert ll.to_array() == [3, 2, 1]
def test_linked_list_iter(self): ll = LinkedList.from_array([1, 2, 3]) assert list(ll) == [1, 2, 3]