forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_search.py
More file actions
48 lines (38 loc) · 929 Bytes
/
linear_search.py
File metadata and controls
48 lines (38 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Author: OMKAR PATHAK
Created On: 1st August 2017
- Best O(1)
- Average O(n)
- Worst O(n)
"""
import inspect
def search(_list, target, initial_position=0):
"""sequential search algorithm
This function returns the position
of the target if found else returns -1
:param _list:
:param target:
:param initial_position: default @ 0
:return:
"""
position = initial_position
while position < len(_list):
if target == _list[position]:
return position
position += 1
return -1
# TODO: Are these necessary?
def time_complexities():
"""
Return information on functions
time complexity
:return: string
"""
return "Best Case: O(1), Average Case: O(n), Worst Case: O(n)"
def get_code():
"""
easily retrieve the source code
of the function
:return: source code
"""
return inspect.getsource(search)