-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear search
More file actions
67 lines (48 loc) · 1.65 KB
/
Linear search
File metadata and controls
67 lines (48 loc) · 1.65 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#original function
number_list = [ 10, 14, 19, 26, 27, 31, 33, 35, 42, 44]
target_number = 33
def linear_search(search_list, target_value):
for idx in range(len(search_list)):
if search_list[idx] == target_value:
return idx
raise ValueError("{0} not in list".format(target_value))
try:
# Call the function below...
print(linear_search(number_list,100))
except ValueError as error_message:
print("{0}".format(error_message))
# finding duplicates
# Search list and target value
tour_locations = [ "New York City", "Los Angeles", "Bangkok", "Istanbul", "London", "New York City", "Toronto"]
target_city = "New York City"
#Linear Search Algorithm
def linear_search(search_list, target_value):
matches = []
for idx in range(len(search_list)):
if search_list[idx] == target_value:
matches.append(idx)
if matches:
return matches
else:
raise ValueError("{0} not in list".format(target_value))
#Function call
tour_stops = linear_search(tour_locations, target_city)
print(tour_stops)
#finding the maximum
# Search list
test_scores = [88, 93, 75, 100, 80, 67, 71, 92, 90, 83]
#Linear Search Algorithm
def linear_search(search_list):
maximum_score_index = None
for idx in range(len(search_list)):
print(search_list[idx])
if maximum_score_index is None or search_list[idx] > search_list[maximum_score_index]:
maximum_score_index = idx
return maximum_score_index
#if search_list[idx] == target_value:
# return idx
#raise ValueError("{0} not in list".format(target_value))
# Function call
highest_score = linear_search(test_scores)
#Prints out the highest score in the list
print(highest_score)