@@ -19,11 +19,10 @@ def binary_search(lst: list[Any], item: Any, start: int, end: int) -> int:
1919 """
2020 Find the insertion position of an item in a sorted list.
2121
22- :param lst: A sorted list of comparable items.
23- :param item: The item to insert.
24- :param start: The starting index of the search range.
25- :param end: The ending index of the search range.
26- :return: The index where the item should be inserted.
22+ >>> binary_search([1, 3, 5, 7], 4, 0, 3)
23+ 2
24+ >>> binary_search([1, 3, 5, 7], 8, 0, 3)
25+ 4
2726 """
2827 if start == end :
2928 return start if lst [start ] > item else start + 1
@@ -43,8 +42,10 @@ def insertion_sort(lst: list[Any]) -> list[Any]:
4342 """
4443 Sort a list using the insertion sort algorithm.
4544
46- :param lst: A list of comparable items.
47- :return: The sorted list.
45+ >>> insertion_sort([3, 2, 1])
46+ [1, 2, 3]
47+ >>> insertion_sort([1])
48+ [1]
4849 """
4950 length = len (lst )
5051
@@ -60,9 +61,12 @@ def merge(left: list[Any], right: list[Any]) -> list[Any]:
6061 """
6162 Merge two sorted lists into a single sorted list.
6263
63- :param left: The left sorted list.
64- :param right: The right sorted list.
65- :return: A merged sorted list.
64+ >>> merge([1, 3, 5], [2, 4, 6])
65+ [1, 2, 3, 4, 5, 6]
66+ >>> merge([], [1, 2])
67+ [1, 2]
68+ >>> merge([1, 2], [])
69+ [1, 2]
6670 """
6771 if not left :
6872 return right
@@ -121,4 +125,8 @@ def main():
121125
122126
123127if __name__ == "__main__" :
128+ import doctest
129+
130+ doctest .testmod ()
131+
124132 main ()
0 commit comments