From e4375b63660f59b882f2c77442ee75652e036913 Mon Sep 17 00:00:00 2001 From: Gabriel Araujo Date: Mon, 9 Oct 2017 12:39:32 -0300 Subject: [PATCH] Added the linear search algorithm example --- SearchingAlgorithms/Linear_Search.py | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/SearchingAlgorithms/Linear_Search.py b/SearchingAlgorithms/Linear_Search.py index 8b13789..171bfda 100644 --- a/SearchingAlgorithms/Linear_Search.py +++ b/SearchingAlgorithms/Linear_Search.py @@ -1 +1,33 @@ +def linear_search(all_items, item): + """Searches an item inside an iterable using the + linear search algorithm. + + Positional Arguments: + all_items -- an iterable of items to search + item -- the item to find + """ + for index, element in enumerate(all_items): + if element == item: + return index + else: + return -1 + + +import random + +# Generating a random list with 25 integers +my_list = [ random.randint(0, 100) for _ in range(25) ] +print("List: ", my_list) + +# Reading the number to find in list +item_to_find = int(input("Type the number to find: ")) + +# Executing the linear search algorithm +found_index = linear_search(my_list, item_to_find) + +# Printing the results +if found_index > -1: + print("The {} was found at position {}!".format(item_to_find, found_index)) +else: + print("The {} wasn't found!".format(item_to_find)) \ No newline at end of file