Skip to content

Commit 35fd0f2

Browse files
Merge pull request seeditsolution#327 from anandsaurav07/master
Linear Search
2 parents 8ad4222 + 6f7f401 commit 35fd0f2

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Linear Search

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def linear_search(alist, key):
2+
"""Return index of key in alist. Return -1 if key not present."""
3+
for i in range(len(alist)):
4+
if alist[i] == key:
5+
return i
6+
return -1
7+
8+
9+
alist = input('Enter the list of numbers: ')
10+
alist = alist.split()
11+
alist = [int(x) for x in alist]
12+
key = int(input('The number to search for: '))
13+
14+
index = linear_search(alist, key)
15+
if index < 0:
16+
print('{} was not found.'.format(key))
17+
else:
18+
print('{} was found at index {}.'.format(key, index))

0 commit comments

Comments
 (0)