Fix doctests

This commit is contained in:
Hardik Pawar 2024-10-03 11:54:07 +05:30
parent fb13521086
commit 1ac1f0c0e4

View File

@ -5,14 +5,6 @@ class Node:
Attributes:
data (int): The data stored in the node.
next (Node | None): A reference to the next node in the linked list.
>>> head = Node(4)
>>> head.next = Node(2)
>>> head.next.next = Node(1)
>>> head.next.next.next = Node(3)
>>> sorted_head = merge_sort_linked_list(head)
>>> print_linked_list(sorted_head)
1 2 3 4
"""
def __init__(self, data: int) -> None:
@ -22,39 +14,63 @@ class Node:
def get_middle(head: Node | None) -> Node | None:
"""
Find the middle node of the linked list using the slow and fast pointer technique.
Find the node before the middle of the linked list
using the slow and fast pointer technique.
Parameters:
head: The head node of the linked list.
Returns:
The middle node of the linked list, or None if the list is empty.
The node before the middle of the linked list,
or None if the list has fewer than 2 nodes.
Example:
>>> head = Node(1)
>>> head.next = Node(2)
>>> head.next.next = Node(3)
>>> middle = get_middle(head)
>>> middle.data if middle else None
>>> middle.data
2
>>> get_middle(None) is None
True
"""
if head is None or head.next is None:
return head
return None
slow: Node = head
fast: Node | None = head.next
slow: Node | None = head
fast: Node | None = head
while fast is not None and fast.next is not None:
if slow is None: # This should never happen, but it satisfies the type checker
return None
slow = slow.next
fast = fast.next.next
return slow
def print_linked_list(head: Node | None) -> None:
"""
Print the linked list in a single line.
Parameters:
head: The head node of the linked list.
Example:
>>> head = Node(1)
>>> head.next = Node(2)
>>> head.next.next = Node(3)
>>> print_linked_list(head)
1 2 3
"""
current = head
first = True # To avoid printing space before the first element
while current:
if not first:
print(" ", end="")
print(current.data, end="")
first = False
current = current.next
print()
def merge(left: Node | None, right: Node | None) -> Node | None:
"""
Merge two sorted linked lists into one sorted linked list.
@ -91,32 +107,6 @@ def merge(left: Node | None, right: Node | None) -> Node | None:
return result
def print_linked_list(head: Node | None) -> None:
"""
Print the linked list in a single line.
Parameters:
head: The head node of the linked list.
Example:
>>> head = Node(1)
>>> head.next = Node(2)
>>> head.next.next = Node(3)
>>> print_linked_list(head)
1 2 3
"""
current = head
first = True # To avoid printing space before the first element
while current:
if not first:
print(" ", end="")
print(current.data, end="")
first = False
current = current.next
print()
def merge_sort_linked_list(head: Node | None) -> Node | None:
"""
Sort a linked list using the Merge Sort algorithm.
@ -135,22 +125,6 @@ def merge_sort_linked_list(head: Node | None) -> Node | None:
>>> sorted_head = merge_sort_linked_list(head)
>>> print_linked_list(sorted_head)
1 2 3 4
>>> head = Node(1)
>>> head.next = Node(2)
>>> head.next.next = Node(3)
>>> head.next.next.next = Node(4)
>>> sorted_head = merge_sort_linked_list(head)
>>> print_linked_list(sorted_head)
1 2 3 4
>>> head = Node(10)
>>> head.next = Node(3)
>>> head.next.next = Node(5)
>>> head.next.next.next = Node(1)
>>> sorted_head = merge_sort_linked_list(head)
>>> print_linked_list(sorted_head)
1 3 5 10
"""
# Base Case: 0 or 1 node
@ -159,17 +133,18 @@ def merge_sort_linked_list(head: Node | None) -> Node | None:
# Split the linked list into two halves
middle = get_middle(head)
if middle is None:
if middle is None or middle.next is None:
return head
next_to_middle = middle.next
middle.next = None # Split the list into two parts
left = merge_sort_linked_list(head) # Sort left half
right = merge_sort_linked_list(next_to_middle) # Sort right half
# Recursively sort both halves
left = merge_sort_linked_list(head)
right = merge_sort_linked_list(next_to_middle)
# Merge sorted halves
sorted_list = merge(left, right)
return sorted_list
return merge(left, right)
if __name__ == "__main__":