-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lecture "Brute-force argorithms", exercise 1 #17
Comments
I tried to code in python and test my code:
Not Found! found Good Omens in position 3 of list of books |
Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 "The Sandman" is not in the list |
Execution for item, position in enumerate(list): if item == value_to search: Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 End of items/iteration |
The algorithm
Our
Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 The execution would continue to the next iteration, but there are no more items in the list. Since there's not an alternative |
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"), Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 end of the list after reaching the end of the list and not having found the value searched, nothing at all or 'none' is returned. |
enumerate(input_list) will result in: Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 |
list_of_books = list (["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]) Foreach loop execution Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 As the iterations show "The Sandman" is not one of the book from the list of books, so if the value to search is not present, Python will return |
def test_linear_search (input_list, value_to_search, expected): def linear_search (input_list, value_to_search): list_of_books = list (["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]) print (test_linear_search(list_of_books, "The Sandman", None)) #for-each loop execution #Iteration1 #Iteration2 #Iteration3 #Iteration4 #Iteration5 |
Given the function
Consequently, Defining the function
It is possible to study the first iteration
Then the second:
Then the third:
Then the fourth:
Lastly, the fifth:
The result is hence |
Linear_search(list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]), "The Sandman") FOR_EACH LOOP EXECUTION: Iter. 1. Iter. 2. Iter. 3. Iter. 4. Iter. 5. |
list_of_books = list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]) #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 1position = Oitem = "Coralione"item == value_to_search is FalseContinue to the next iterationIteration 2position = 1item = "American Gods"item == value_to_search is FalseContinue to the next iterationIteration 3position = 2item = "The Graveyard Book"item == value_to_search is FalseContinue to the next iterationIteration 4position = 3item = "Good Omens"item == value_to_search is FalseContinue to the next iterationIteration 5position = 4item = "Neverwhere"item == value_to_search is FalseContinue to the next iteration#Since the value_to_search was not found the algorithm will return "None" |
Task_1 FOR-EACH LOOP EXECUTION Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 Return nothing (as the items are finished and the value_to_search was not found). |
list("Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere") FOR-EACH LOOP EXECUTION Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 return None |
List_of_books = list([“Coraline”, “American Gods”, “The Graveyard Book”, “Good omens”, “Neverwhere”]) linear_search(list_of_books, “The Sandman”) this means that our value_to_search is “The Sandman” enumerate(list_of_books) will result in: Iteration 1 The value_to_search was not found this means that the algorithm will return the value none. |
linear_search(list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]), "The Sandman") enumarate(linear_search) --> it result in (enumarate[(0, "Coraline"), (1, "American Gods"), (2, "The Graveyard Book"), (3, "Good Omens"), (4, "Neverwhere")]) Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 All the items have been processed, the iteration ends. None of the items is "The Sandman". Python will return "None" by default. |
|
execution: Iteration: iteration 2 iteration 3 iteration 4 iteration 5 value_to search = "The Sandman" |
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.The text was updated successfully, but these errors were encountered: