Skip to content

Commit b83ae99

Browse files
Ashley-J-Georgecclauss
authored andcommitted
Update linear_search.py (TheAlgorithms#2422)
* Update linear_search.py Python implementation of recursive linear search algorithm * Update linear_search.py Added different doctests Added the parameter hints Handled the exception * Update linear_search.py added parameter hints to linear_search * Update linear_search.py Both the functions return the index if the target is found and -1 if it is not found The rec_linear_search raises an exception if there is an indexing problem Made changes in the doc comments * Update linear_search.py Co-authored-by: Christian Clauss <[email protected]>
1 parent f1a0227 commit b83ae99

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

Diff for: searches/linear_search.py

+42-16
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
This is pure Python implementation of linear search algorithm
33
44
For doctests run following command:
5-
python -m doctest -v linear_search.py
6-
or
75
python3 -m doctest -v linear_search.py
86
97
For manual testing run:
10-
python linear_search.py
8+
python3 linear_search.py
119
"""
1210

1311

14-
def linear_search(sequence, target):
15-
"""Pure implementation of linear search algorithm in Python
12+
def linear_search(sequence: list, target: int) -> int:
13+
"""A pure Python implementation of a linear search algorithm
1614
1715
:param sequence: a collection with comparable items (as sorted items not required
1816
in Linear Search)
@@ -22,30 +20,58 @@ def linear_search(sequence, target):
2220
Examples:
2321
>>> linear_search([0, 5, 7, 10, 15], 0)
2422
0
25-
2623
>>> linear_search([0, 5, 7, 10, 15], 15)
2724
4
28-
2925
>>> linear_search([0, 5, 7, 10, 15], 5)
3026
1
31-
3227
>>> linear_search([0, 5, 7, 10, 15], 6)
33-
28+
-1
3429
"""
3530
for index, item in enumerate(sequence):
3631
if item == target:
3732
return index
38-
return None
33+
return -1
34+
35+
36+
def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int:
37+
"""
38+
A pure Python implementation of a recursive linear search algorithm
39+
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
46+
47+
Examples:
48+
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0)
49+
0
50+
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700)
51+
4
52+
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30)
53+
1
54+
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6)
55+
-1
56+
"""
57+
if not (0 <= high < len(sequence) and 0 <= low < len(sequence)):
58+
raise Exception("Invalid upper or lower bound!")
59+
if high < low:
60+
return -1
61+
if sequence[low] == target:
62+
return low
63+
if sequence[high] == target:
64+
return high
65+
return rec_linear_search(sequence, low + 1, high - 1, target)
3966

4067

4168
if __name__ == "__main__":
4269
user_input = input("Enter numbers separated by comma:\n").strip()
43-
sequence = [int(item) for item in user_input.split(",")]
70+
sequence = [int(item.strip()) for item in user_input.split(",")]
4471

45-
target_input = input("Enter a single number to be found in the list:\n")
46-
target = int(target_input)
72+
target = int(input("Enter a single number to be found in the list:\n").strip())
4773
result = linear_search(sequence, target)
48-
if result is not None:
49-
print(f"{target} found at position : {result}")
74+
if result != -1:
75+
print(f"linear_search({sequence}, {target}) = {result}")
5076
else:
51-
print("Not found")
77+
print(f"{target} was not found in {sequence}")

0 commit comments

Comments
 (0)