diff --git a/algorithms/Python/searching/linear_search.py b/algorithms/Python/searching/linear_search.py index ded48038..5c401107 100644 --- a/algorithms/Python/searching/linear_search.py +++ b/algorithms/Python/searching/linear_search.py @@ -1,12 +1,17 @@ -def linear_search(a, x): - for i in range(len(a)): - if a[i] == x: +arr = [1, 4, 7, 9, 14, 17, 39, 56] +targets = (8, 39) + + +def linear_search(arr, target): + """ + >>> all(linear_search(arr, x) == arr.index(x) if x in arr else -1 for x in targets) + True + """ + for i, item in enumerate(arr): + if item == target: return i return -1 - -a = [1,4,7,9,14,17,39,56] -x = 8 -y = 39 -print(linear_search(a,x)) -print(linear_search(a,y)) + +for target in targets: + print(f"linear_search({arr}, {target}) = {linear_search(arr, target)}")