2
2
This is pure Python implementation of linear search algorithm
3
3
4
4
For doctests run following command:
5
- python -m doctest -v linear_search.py
6
- or
7
5
python3 -m doctest -v linear_search.py
8
6
9
7
For manual testing run:
10
- python linear_search.py
8
+ python3 linear_search.py
11
9
"""
12
10
13
11
14
12
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
16
14
17
15
:param sequence: a collection with comparable items (as sorted items not required
18
16
in Linear Search)
@@ -22,13 +20,10 @@ def linear_search(sequence: list, target: int) -> int:
22
20
Examples:
23
21
>>> linear_search([0, 5, 7, 10, 15], 0)
24
22
0
25
-
26
23
>>> linear_search([0, 5, 7, 10, 15], 15)
27
24
4
28
-
29
25
>>> linear_search([0, 5, 7, 10, 15], 5)
30
26
1
31
-
32
27
>>> linear_search([0, 5, 7, 10, 15], 6)
33
28
-1
34
29
"""
@@ -40,7 +35,7 @@ def linear_search(sequence: list, target: int) -> int:
40
35
41
36
def rec_linear_search (sequence : list , low : int , high : int , target : int ) -> int :
42
37
"""
43
- Pure implementation of recursive linear search algorithm in Python
38
+ A pure Python implementation of a recursive linear search algorithm
44
39
45
40
:param sequence: a collection with comparable items (as sorted items not required
46
41
in Linear Search)
@@ -52,19 +47,15 @@ def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int:
52
47
Examples:
53
48
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0)
54
49
0
55
-
56
50
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700)
57
51
4
58
-
59
52
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30)
60
53
1
61
-
62
54
>>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6)
63
55
-1
64
56
"""
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 )):
66
58
raise Exception ("Invalid upper or lower bound!" )
67
-
68
59
if high < low :
69
60
return - 1
70
61
if sequence [low ] == target :
@@ -76,12 +67,11 @@ def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int:
76
67
77
68
if __name__ == "__main__" :
78
69
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 ("," )]
80
71
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 ())
83
73
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 } " )
86
76
else :
87
- print ("Not found" )
77
+ print (f" { target } was not found in { sequence } " )
0 commit comments