Skip to content

Commit 1c57978

Browse files
committed
Fix: improve docstrings and use ValueError in rec_linear_search
1 parent 791deb4 commit 1c57978

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

searches/linear_search.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@
1212
def linear_search(sequence: list, target: int) -> int:
1313
"""A pure Python implementation of a linear search algorithm
1414
15-
:param sequence: a collection with comparable items (sorting is not required for
16-
linear search)
17-
:param target: item value to search
18-
:return: index of found item or -1 if item is not found
15+
:param sequence: a collection with comparable items (No sorting required)
16+
:param target: Value to search for
17+
:return: index of target if found, else -1
1918
2019
Examples:
2120
>>> linear_search([0, 5, 7, 10, 15], 0)
2221
0
23-
>>> linear_search([0, 5, 7, 10, 15], 15)
24-
4
25-
>>> linear_search([0, 5, 7, 10, 15], 5)
26-
1
2722
>>> linear_search([0, 5, 7, 10, 15], 6)
2823
-1
2924
"""
@@ -35,14 +30,13 @@ def linear_search(sequence: list, target: int) -> int:
3530

3631
def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int:
3732
"""
38-
A pure Python implementation of a recursive linear search algorithm
33+
Recursive linear search algorithm
3934
40-
:param sequence: a collection with comparable items (as sorted items not required
41-
in Linear Search)
42-
:param low: Lower bound of the array
43-
:param high: Higher bound of the array
44-
:param target: The element to be found
45-
:return: Index of the key or -1 if key not found
35+
:param sequence: Collection of comparable items (no sorting required)
36+
:param low: Lower index bound
37+
:param high: Higher index bound
38+
:param target: Value to search for
39+
:return: Index of the target if found, else -1
4640
4741
Examples:
4842
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0)
@@ -55,7 +49,7 @@ def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int:
5549
-1
5650
"""
5751
if not (0 <= high < len(sequence) and 0 <= low < len(sequence)):
58-
raise Exception("Invalid upper or lower bound!")
52+
raise ValueError("Invalid upper or lower bound!")
5953
if high < low:
6054
return -1
6155
if sequence[low] == target:

0 commit comments

Comments
 (0)