Open essepuntato opened 4 years ago
def linear_search(input_list, value_to_search):
for position, item in enumerate(input_list):
if item == value_to_search:
return position
linear_search(list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]), "The Sandman")
# FOR-EACH LOOP EXECUTION
# enumerate(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]) will result in:
# enumerate([(0, "Coraline"), (1, "American Gods"), (2, "The Graveyard Book"), (3, "Good Omens"), (4, "Neverwhere")])
#
# Iteration 1
# position = 0
# item = "Coraline"
# item == value_to_search is False
# Continue to the next iteration
#
# Iteration 2
# position = 1
# item = "American Gods"
# item == value_to_search is False
# Continue to the next iteration
#
# Iteration 3
# position = 2
# item = "The Graveyard Book"
# item == value_to_search is False
# Continue to the next iteration
#
# Iteration 4
# # position = 3
# # item = "Good Omens"
# # item == value_to_search is False
# # Continue to the next iteration
#
# Iteration 5
# position = 4
# item = "Neverwhere"
# item == value_to_search is False
#
# The item was not found. Return None
This the solution found by @elisasilvad and me.
def linear_search(input_list, value_to_search): for position, item in enumerate(book_list): if item == "The Sandman" return position
-enumerate(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]) -enumerate([(0, "Coraline"), (1, "American Gods"), (2, "The Graveyard Book"), (3, "Good Omens"), (4, "Neverwhere")]) -in the first string, for starts the foreach loop while enumerate() creates n tuples from the list (first element of each tuple will be a number, starting from 0; second element will be the title in the list's order, presented as a string). -at this point, before returning "none"/not executing 'return', the algorithm will iterate five times.
-First Iteration position = 0, item = "Coraline" item == "The Sandman" is False unable to return position -Second Iteration position = 1, item = "American Gods" item == "The Sandman" is False unable to return position -Third Iteration position = 2, item = "The Graveyard Book" item == "The Sandman" is False unable to return position -Fourth Iteration position = 3, item = "Good Omens" item == "The Sandman"is False unable to return position -Fifth Iteration position = 4, item = "Neverwhere" item == "The Sandman" is False unable to return position
-in case we would like a different output, as Arcangelo suggested me, we could add, after return, an else possibility which prints "Item is not in the list"
Hi guys,
Thanks for your answers. You can find my solution online. Just a few notes:
I've seen that some of you have changed the input material I've introduced in the exercise, and you have returned the appropriate result in that case. This is fine, of course. However, please, I would ask you to answer explicitly to the exercise first and, then, if you want to experiment another run, do it by changing the input.
Remember to explicitly say which value is returned in any case. For instance, if no return statement will be executed by running the function, say explicitly that None
will be returned.
The output is None
Write down the execution steps of
linear_search(list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]), "The Sandman")
, as explained in Listing 7.