Skip to content

Commit 92fdb7d

Browse files
author
Shreya
committed
Add doctests to helper function docstrings
1 parent 8af4051 commit 92fdb7d

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

sorts/tim_sort.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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,12 @@ 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]
49+
>>> insertion_sort([])
50+
[]
4851
"""
4952
length = len(lst)
5053

@@ -60,9 +63,12 @@ def merge(left: list[Any], right: list[Any]) -> list[Any]:
6063
"""
6164
Merge two sorted lists into a single sorted list.
6265
63-
:param left: The left sorted list.
64-
:param right: The right sorted list.
65-
:return: A merged sorted list.
66+
>>> merge([1, 3, 5], [2, 4, 6])
67+
[1, 2, 3, 4, 5, 6]
68+
>>> merge([], [1, 2])
69+
[1, 2]
70+
>>> merge([1, 2], [])
71+
[1, 2]
6672
"""
6773
if not left:
6874
return right

0 commit comments

Comments
 (0)