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: Attributes:
data (int): The data stored in the node. data (int): The data stored in the node.
next (Node | None): A reference to the next node in the linked list. 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: def __init__(self, data: int) -> None:
@ -22,39 +14,63 @@ class Node:
def get_middle(head: Node | None) -> Node | None: 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: Parameters:
head: The head node of the linked list. head: The head node of the linked list.
Returns: 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: Example:
>>> head = Node(1) >>> head = Node(1)
>>> head.next = Node(2) >>> head.next = Node(2)
>>> head.next.next = Node(3) >>> head.next.next = Node(3)
>>> middle = get_middle(head) >>> middle = get_middle(head)
>>> middle.data if middle else None >>> middle.data
2 2
>>> get_middle(None) is None
True
""" """
if head is None or head.next is None: 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: 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 slow = slow.next
fast = fast.next.next fast = fast.next.next
return slow 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: def merge(left: Node | None, right: Node | None) -> Node | None:
""" """
Merge two sorted linked lists into one sorted linked list. 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 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: def merge_sort_linked_list(head: Node | None) -> Node | None:
""" """
Sort a linked list using the Merge Sort algorithm. 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) >>> sorted_head = merge_sort_linked_list(head)
>>> print_linked_list(sorted_head) >>> print_linked_list(sorted_head)
1 2 3 4 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 # 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 # Split the linked list into two halves
middle = get_middle(head) middle = get_middle(head)
if middle is None: if middle is None or middle.next is None:
return head return head
next_to_middle = middle.next next_to_middle = middle.next
middle.next = None # Split the list into two parts middle.next = None # Split the list into two parts
left = merge_sort_linked_list(head) # Sort left half # Recursively sort both halves
right = merge_sort_linked_list(next_to_middle) # Sort right half left = merge_sort_linked_list(head)
right = merge_sort_linked_list(next_to_middle)
# Merge sorted halves # Merge sorted halves
sorted_list = merge(left, right) return merge(left, right)
return sorted_list
if __name__ == "__main__": if __name__ == "__main__":