-
-
Save Edald123/64695796eece5666408c09d7f9c12292 to your computer and use it in GitHub Desktop.
Linear search in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... | |
result = linear_search(number_list, target_number) | |
print("Linear search:", result) | |
except ValueError as error_message: | |
print("{0}".format(error_message)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment