-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Linear_Search.py
More file actions
53 lines (44 loc) · 1.28 KB
/
Python_Linear_Search.py
File metadata and controls
53 lines (44 loc) · 1.28 KB
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
49
50
51
52
53
# Mohammad Hossein Zehtab
# Advanced_Python_Wednesdays
# Project01: Python_Linear_Search_Function
def test(did_pass):
"""
Print the result of a test.
"""
if did_pass:
print('True')
else:
print('Failed')
def search_linear(xs : list, target : str|int|float) -> int:
"""
Searching sequence xs for target and returning its position (index),
returning -1 if target does not exist in sequence xs.
Parameters
----------
xs : list
A sequence (e.g., a list) containing the items to be searched.
target : str|int|float
The item you are trying to find within the sequence.
Returns
-------
int
The index (position) of the target item within the sequence xs if found.
"""
index = 0
for item in xs:
if str(item) == str(target):
return index
index += 1
return -1
def test_suite():
"""
Run the suite of tests for code in this module.
"""
friends = ["Mohammad", "Ali", "Fatemeh", "Hassan", "Hossein", "Zeinab",
"Kulthum"]
test(search_linear(friends, "Ali") == 1)
test(search_linear(friends, "Mohammad") == 0)
test(search_linear(friends, "Kulthum") == 6)
test(search_linear(friends, "Abbas") == -1)
### Driver Code ###
test_suite()