Skip to content

Commit 8520694

Browse files
authored
Update linear_search.py
1 parent dbf677f commit 8520694

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

searches/linear_search.py

Lines changed: 9 additions & 19 deletions
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

1412
def linear_search(sequence: list, target: int) -> int:
15-
"""Pure implementation of linear search algorithm in Python
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,13 +20,10 @@ def linear_search(sequence: list, target: int) -> int:
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)
3328
-1
3429
"""
@@ -40,7 +35,7 @@ def linear_search(sequence: list, target: int) -> int:
4035

4136
def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int:
4237
"""
43-
Pure implementation of recursive linear search algorithm in Python
38+
A pure Python implementation of a recursive linear search algorithm
4439
4540
:param sequence: a collection with comparable items (as sorted items not required
4641
in Linear Search)
@@ -52,19 +47,15 @@ def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int:
5247
Examples:
5348
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0)
5449
0
55-
5650
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700)
5751
4
58-
5952
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30)
6053
1
61-
6254
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6)
6355
-1
6456
"""
65-
if (0 > high or high >= len(sequence)) or (0 > low or low >= len(sequence)):
57+
if not (0 <= high < len(sequence) and 0 <= low < len(sequence)):
6658
raise Exception("Invalid upper or lower bound!")
67-
6859
if high < low:
6960
return -1
7061
if sequence[low] == target:
@@ -76,12 +67,11 @@ def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int:
7667

7768
if __name__ == "__main__":
7869
user_input = input("Enter numbers separated by comma:\n").strip()
79-
sequence = [int(item) for item in user_input.split(",")]
70+
sequence = [int(item.strip()) for item in user_input.split(",")]
8071

81-
target_input = input("Enter a single number to be found in the list:\n")
82-
target = int(target_input)
72+
target = int(input("Enter a single number to be found in the list:\n").strip())
8373
result = linear_search(sequence, target)
84-
if result is not None:
85-
print(f"{target} found at position : {result}")
74+
if result != -1:
75+
print(f"linear_search({sequence}, {target}) = {result}")
8676
else:
87-
print("Not found")
77+
print(f"{target} was not found in {sequence}")

0 commit comments

Comments
 (0)