PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#Python cde to implement Linear Search to a list
def linear_search(alist, key):
"""Return index of key in alist. Return -1 if key not present"""
for i in range(len(alist)):
if alist[i] == key:
return i
return -1
alist = input('Enter the list of numbers: ')
alist = alist.split()
alist = [int(x) for x in alist]
key = int(input('The number to search for: '))
index = linear_search(alist, key)
if index < 0:
print('{} was not found.'.format(key, index))
else:
print('{} was found at index {}.'.format(key, index))
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run