-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_search.py
More file actions
40 lines (29 loc) · 856 Bytes
/
linear_search.py
File metadata and controls
40 lines (29 loc) · 856 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
import random
# linear search
def sequentialSearch(alist, item):
pos = 0
found = False
while pos < len(alist) and not found:
if alist[pos] == item:
found = True
else:
pos = pos+1
return found
testlist = [1, 2, 32, 8, 17, 19, 42, 13, 0]
print(sequentialSearch(testlist, 3))
print(sequentialSearch(testlist, 13))
#linear search example #2
section12A=[80,75,88,80,70,98]
section12B=[85,80,93,85,75,100]
section12C=[75,70,83,75,65,93]
section12D=[70,65,78,70,60,88]
def search(myList,searchValue):
found = False
# use list parameter not the actual list name
for i in range(len(myList)):
if myList[i]==searchValue:
found=True
print("The list contains a 90",found)
search(section12A,98)
search(section12B,77)
search(section12C,88)