-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_search_v3.py
31 lines (22 loc) · 987 Bytes
/
linear_search_v3.py
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
##############################################################################################################
#
# Linear Search - VERSION 3: USING A WHILE LOOP
#
##############################################################################################################
def searching(list, size, number):
i=0 #index 0
while i < size and list[i] != number: #loop stops if checked the whole list or found the target number
i = i+1 #check the next number
if i == size: # if the search is done through all the elements and the number is not found
return -1 #
return i #else, the return the element found
list = [1,2,7,0,5,6,8,9,7]
number = 7
size = len(list)
# Calling the function and saving in the variable result
result = searching(list, size, number)
if result == -1:
print("Number is not present in list")
else:
print(f"Number number {number} is present at index", result)
# OUTPUT =>> Number number 7 is present at index 2