def test_remove_index(self): ll = LinkedList() ll.add(1) ll.add(2) ll.remove_item_at(0) self.assertEqual(ll.get_item_at(0), 2)
def test_remove_item(self): ll = LinkedList() ll.add(1) ll.add(2) self.assertEqual(len(ll), 2) ll.remove(1) self.assertEqual(len(ll), 1)
def test_linked_list_contains(self) -> None: linked_list = LinkedList() linked_list.add(10) linked_list.add(20) linked_list.add(30) assert (linked_list.contains(30) is True) assert (linked_list.contains(10) is True) assert (linked_list.contains(20) is True) assert (linked_list.contains(50) is False)
def test_linked_list_remove(self) -> None: # Remove from the head linked_list = LinkedList() linked_list.add(10) linked_list.add(20) linked_list.add(30) linked_list.add(40) linked_list.add(50) linked_list.add(60) linked_list.add(70) assert (linked_list.size == 7) linked_list.remove(10) assert (linked_list.size == 6) assert (linked_list.contains(10) is False) # Remove from the middle assert (linked_list.size == 6) linked_list.remove(40) assert (linked_list.size == 5) assert (linked_list.contains(40) is False) # Remove from the tail assert (linked_list.size == 5) linked_list.remove(70) assert (linked_list.size == 4) assert (linked_list.contains(70) is False)
def test_linked_list_clear(self) -> None: # Clear empty list linked_list = LinkedList() linked_list.clear() assert (linked_list.size == 0) linked_list.add(10) linked_list.clear() assert (linked_list.size == 0) linked_list.add(10) linked_list.add(20) linked_list.clear() assert (linked_list.size == 0) linked_list.add(10) linked_list.add(20) linked_list.add(30) linked_list.add(40) linked_list.add(50) linked_list.clear() assert (linked_list.size == 0)
def test_linked_list_add(self) -> None: linked_list = LinkedList() linked_list.add(10) assert (linked_list.size == 1) linked_list.add(20) assert (linked_list.size == 2) linked_list.add(30) assert (linked_list.size == 3)
def merge(left, right): merged = LinkedList() merged.add(0) current = merged.head left_head = left.head right_head = right.head while left_head or right_head: # left_head [None] rigth_head [head 1 => 2 => None] if left_head is None: current.next_node = right_head right_head = right_head.next_node # left_head [head 1 => 2 => None] rigth_head [None] elif right_head is None: current.next_node = left_head left_head = left_head.next_node else: # left_head [head 5 => 2 => None] rigth_head [head 3 => 4 => 1 => None] left_data = left_head.data right_data = right_head.data if left_data < right_data: current.next_node = left_head left_head = left_head.next_node else: current.next_node = right_head right_head = right_head.next_node current = current.next_node head = merged.head.next_node merged.head = head return merged
def test_single_item(self): ll = LinkedList() ll.add(1) self.assertEqual(len(ll), 1)
class TestLinkedList(TestCase): def setUp(self): self.ll = LinkedList() def tearDown(self): pass def test_add_len(self): self.ll.add("one") self.ll.add("two") self.ll.add("three") self.assertEqual(self.ll.head.data, "three", "Last node added is 3") self.assertEqual(len(self.ll), 3, "length of linked list should be 3") def test_get(self): self.ll.add("one") self.ll.add("two") self.ll.add("three") self.assertEqual(self.ll[1], "two", "getter of 1st index should return two") self.assertEqual(self.ll[0], "three", "getter of 0th index should return three") def test_set(self): self.ll[0] = "apple" self.assertEqual(self.ll[0], "apple", "0th index should contain apple") self.ll.add("banana") self.ll.add("pear") self.ll[1] = "mango" self.assertEqual(self.ll[1], "mango", "1st index should contain mango") def test_remove(self): self.ll.add("apple") self.ll.add("banana") self.ll.add("pear") self.assertEqual(len(self.ll), 3, "linked list should initially have length of 3") self.ll.remove("banana") self.assertEqual(self.ll[1], "apple", "1st index of linked list should be apple") self.assertEqual(self.ll[0], "pear", "0th index of linked list should be pear") self.assertEqual(len(self.ll), 2, "new length of linked list should be 2") def test_find(self): self.ll.add("apple") self.ll.add("banana") self.ll.add("pear") self.assertEqual(self.ll.find("banana"), 1, "banana should be located at 1st index") self.assertEqual(self.ll.find("apple"), 2, "apple should be located at 2nd index")
left_data = left_head.data right_data = right_head.data if left_data < right_data: current.next_node = left_head left_head = left_head.next_node else: current.next_node = right_head right_head = right_head.next_node current = current.next_node head = merged.head.next_node merged.head = head return merged l = LinkedList() l.add(10) l.add(2) l.add(44) l.add(15) l.add(200) print(l) sort_linked_list = merge_sort(l) print(sort_linked_list)
class TestLinkedList(TestCase): faker = Faker() list: LinkedList num_nodes = 5 def setUp(self): nodes = [Node(x) for x in range(0, self.num_nodes)] self.list = LinkedList() for node in nodes: self.list.add(node) def test_add_node(self): random_node = Node(self.faker.sentence()) self.list.add(random_node) last_node = self.list.find_last() self.assertEqual( last_node, random_node, f'Last node: {last_node} does not equal {random_node}.' ) def test_add_first_node(self): random_node = Node(self.faker.sentence()) self.list.add_first(random_node) self.assertEqual( self.list.head, random_node, f'Head node of list: {self.list.head} does not equal {random_node}' ) def test_traverse(self): counter = 0 for node in self.list.traverse(): self.assertEqual( node.data, counter, f'Node data {node.data} does not equal {counter}.' ) counter += 1 def test_add_after(self): random_index = random.randint(0, self.num_nodes - 1) node = self.list.find(Node(random_index)) random_node = Node(random.randint(self.num_nodes + 1, 100)) self.list.add_after(node, random_node) self.assertEqual( node.next, random_node, f'Next node of {node} does not equal {random_node}' ) def test_add_before(self): random_index = random.randint(0, self.num_nodes - 1) node = self.list.find(Node(random_index)) random_node = Node(random.randint(self.num_nodes + 1, 100)) self.list.add_before(node, random_node) self.assertEqual( random_node.next.data, node.data, f'Next node of {random_node}: {random_node.next} ' f'does not equal {node}' ) def test_find(self): random_index = random.randint(0, self.num_nodes - 1) random_node_to_find = Node(random_index) found_node = self.list.find(random_node_to_find) self.assertEqual( random_node_to_find.data, found_node.data, f'Found Node {found_node.data} is not equal ' f'to {random_node_to_find.data}' ) def test_find_non_existent(self): random_non_existent_index = random.randint(self.num_nodes + 1, 1000) res_should_be_none = self.list.find(Node(random_non_existent_index)) self.assertIsNone( res_should_be_none, f'Result: {res_should_be_none} is not None as expected.' ) def test_find_last(self): last_node = Node(self.num_nodes - 1) found_node = self.list.find_last() self.assertEqual( last_node.data, found_node.data, f'Last node {found_node} not equal to expected {last_node}' ) def test_find_before(self): random_index_not_head = random.randint(1, self.num_nodes - 1) random_node = Node(random_index_not_head) node_before = self.list.find_before(random_node) self.assertEqual( node_before.data, random_index_not_head - 1, f'Node before {node_before.data} is not equal ' f'to {random_index_not_head - 1}' ) def test_delete(self): random_node = Node(random.randint(0, self.num_nodes - 1)) self.list.delete(random_node) should_be_none = self.list.find(random_node) self.assertIsNone( should_be_none, f'{should_be_none} is not None as expected.' )
if k == 0: return linked original_head = linked.head new_head = linked[k] original_tail = linked.tail new_tail = linked[(k - 1) % length_linked] linked.head = new_head original_tail.next = original_head new_tail.next = None linked.tail = new_tail return linked if __name__ == '__main__': a = Node('a') b = Node('b') c = Node('c') d = Node('d') e = Node('e') linky = LinkedList(a) linky.add(b) linky.add(c) linky.add(d) linky.add(e) print(rotate(linky, 3))