def test_reverse(self): dll = LinkedList() items = 100 assert dll.is_empty() assert dll.reverse() is None assert dll.is_empty() for i in range(items): assert dll.append(i) is None assert isinstance(dll.head, Node) assert isinstance(dll.tail, Node) assert dll.size == items assert dll.head is not dll.tail assert dll.head.data == 0 assert dll.head.prev is None assert dll.tail.data == items - 1 assert dll.tail.next is None assert dll.reverse() is None assert isinstance(dll.head, Node) assert isinstance(dll.tail, Node) assert dll.size == items assert dll.head is not dll.tail assert dll.head.data == items - 1 assert dll.head.prev is None assert dll.tail.data == 0 assert dll.tail.next is None match = items - 1 for node in dll: assert node.data == match match -= 1 assert dll.reverse() is None match = 0 for node in dll: assert node.data == match match += 1
def test_sublist_halves(self): dll = LinkedList() items = 10 assert dll.head is None assert dll.size == 0 for i in range(items): assert dll.push(i) is None dll.reverse() def halves(linked_list, begin, end): mid = linked_list.size // 2 left = sublist(linked_list, begin, mid) right = sublist(linked_list, mid, end) return left, right def print_list(linked_list): for node in linked_list: print(node.data, end=',') print(" ") def check(linked_list): if linked_list.size <= 2: return left, right = halves(linked_list, 0, linked_list.size) if linked_list.size % 2: # Odds assert left.size == (linked_list.size // 2) assert right.size == (linked_list.size // 2 + 1) else: # Evens assert left.size == (linked_list.size // 2) assert right.size == (linked_list.size // 2) print_list(left) print_list(right) check(left) check(right) check(dll)