We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 8ad4222 + 6f7f401 commit 35fd0f2Copy full SHA for 35fd0f2
Linear Search
@@ -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