Open essepuntato opened 1 year ago
list_of_books = list() list_of_books.append("Coraline") list_of_books.append("American Gods") list_of_books.append("The Graveyard Book") list_of_books.append("Good Omens") list_of_books.append("Neverwhere") linear_search(list_of_books, "The Sandman") enumerate(list_of_books) for series in list_of_books: print(list_of_books)
#
#
#
#
`list_of_books = list (["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"])
linear_search (list_of_books, "The Sandman")
FOR-EACH LOOP EXECUTION enumerate (input_list) 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 Return None`
list_of_books = list(["Coraline", "American Gods",
"The Graveyard Book", "Good Omens",
"Neverwhere"])
linear_search(list_of_books, "The Sandman")
# FOR-EACH LOOP EXECUTION
# enumerate(input_list) 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
# Return the position (i.e. 2) and end the execution of the algorithm
# Iteration 4
# position = 3
# item = "Good Omens"
# item == value_to_search is False
# Continue to the next iteration
# Iteration 4
# position = 3
# item = "Neverwhere"
# item == value_to_search is False
# Reached the end of the list of tuples iterated by the foreach loop
# -> exit the foreach loop
# Reached the end of the function without an explicit return command
# -> implicitly set None as the return value
# return None
list_of_books = list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]), "The Sandman")
linear_search(list_of_books, "The Sandman")
For_each LOOP EXECUTION enumerate(input_list) 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 != Search_value
Iteration 2 Position 1 Item == "American Gods" Item != Search_value
Iteration 3 Position 2 Item == "The Graveyard Book" Item != Search_value
Iteration 4 Position 3 Item == "Good Omens" Item != Search_value
Iteration 5 Position 4 Item == "Neverwhere" Item != Search_value
Reached the end of the values for the FOR each; exit the For each loop: no Search_value has been found, return None
list_of_books = list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"])
linear_search(list_of_books, "The Sandman")
# FOR-EACH LOOP EXECUTION
# enumerate(input_list) 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
# Return None and end the execution of the algorithm
linear_search(list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]), "The Sandman")
linear_search (list_of_books, "The Sandman")
FOR-EACH LOOP EXECUTION enumerate (input_list) 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
Return None
list_of_books = list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"])
linear_search(list_of_books, "The Sandman")
# FOR-EACH LOOP EXECUTION
# enumerate(input_list) 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
# Return "None"
list_of_books = list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"])
linear_search(list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]), "The Sandman")
#
#
#
list_of_books = list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"])
linear_search(list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]), "The Sandman")
#
#
#
list_of_books = list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"])
linear_search(list_of_books, "American Gods")
print(linear_search(list_of_books, "The Sandman"))
# FOR-EACH LOOP EXECUTION
# enumerate(input_list) 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
# Return None and end the execution of the algorithm
#
# Once reached the end of all the possible iterations, the value to search is not present in the list, so Python will automatically return the object None as a result and end the execution of the algorithm
list_of_books = list(["Coraline","American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"])
linear_search(["Coraline","American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"],"The Sandman")
# for each execution loop
# enumerate (input_list) 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
#
# return None
list_of_books = (list(["Coraline", "American gods", "The Graveyard book", "Good omens", "Neverwhere"]) , "The Sandman") enumerate(list_of_books, "The Sandaman") enumerate ([(0, "Coraline"), (1, "American gods"), (2, "The Graveyard book"), (3,"Good omens"), (4, "Neverwhere")])
linear_search(list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]), "The Sandman")
# 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
# Reached the end of the iterators, value to search is not present in the list.
# Exit for loop and return None.
list_of_books = list (["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"])
linear_search (list_of_books, "The Sandman")
For-Each Loop Execution enumerate(input_list) 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 Return None
Dear all,
Please, in your answers to the various exercises online, if you have to write Python code, be sure that the correct indent is preserved by previewing your post before publishing it. You can use the ``` environment for defining your Python code:
``` write your Python code here ```
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.