Skip to content

Commit a2dd3c3

Browse files
Enhance sentinel_linear_search doctests with edge cases and type variations
1 parent dda99e6 commit a2dd3c3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

searches/sentinel_linear_search.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ def sentinel_linear_search(sequence, target):
1818
:param target: item value to search
1919
:return: index of found item or None if item is not found
2020
21+
The sentinel linear search algorithm works by appending the target value to the
22+
end of the list before searching. This eliminates the need for boundary checking
23+
during the loop, as the search is guaranteed to find the target (at minimum, at
24+
the end where the sentinel is placed).
25+
2126
Examples:
2227
>>> sentinel_linear_search([0, 5, 7, 10, 15], 0)
2328
0
@@ -30,6 +35,35 @@ def sentinel_linear_search(sequence, target):
3035
3136
>>> sentinel_linear_search([0, 5, 7, 10, 15], 6)
3237
38+
>>> sentinel_linear_search([1], 1)
39+
0
40+
41+
>>> sentinel_linear_search([1], 99)
42+
43+
>>> sentinel_linear_search([], 5)
44+
45+
>>> sentinel_linear_search([3, 1, 4, 1, 5, 9, 2, 6], 9)
46+
5
47+
48+
>>> sentinel_linear_search([3, 1, 4, 1, 5, 9, 2, 6], 1)
49+
1
50+
51+
>>> sentinel_linear_search(['a', 'b', 'c', 'd'], 'c')
52+
2
53+
54+
>>> sentinel_linear_search(['a', 'b', 'c', 'd'], 'z')
55+
56+
>>> sentinel_linear_search([1.5, 2.7, 3.14, 4.0], 3.14)
57+
2
58+
59+
>>> sentinel_linear_search([1.5, 2.7, 3.14, 4.0], 2.5)
60+
61+
>>> sentinel_linear_search([-10, -5, 0, 5, 10], -5)
62+
1
63+
64+
>>> sentinel_linear_search([-10, -5, 0, 5, 10], 0)
65+
2
66+
3367
"""
3468
sequence.append(target)
3569

0 commit comments

Comments
 (0)