comp-think / 2019-2020

The GitHub repository containing all the material related to the Computational Thinking and Programming course of the Digital Humanities and Digital Knowledge degree at the University of Bologna (a.a. 2019/2020).
Other
12 stars 3 forks source link

Lecture "Brute-force algorithms", exercise 1 #16

Open essepuntato opened 4 years ago

essepuntato commented 4 years ago

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.

arcangelo7 commented 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
FrancescaGenovese commented 4 years ago

esercizio python This the solution found by @elisasilvad and me.

sntcristian commented 4 years ago

Screenshot (5)

FrancescoFernicola commented 4 years ago

Screenshot 2019-11-06 00 46 08 Screenshot 2019-11-06 00 46 25

aschimmenti commented 4 years ago

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"

noreanystrom commented 4 years ago

image

essepuntato commented 4 years ago

Hi guys,

Thanks for your answers. You can find my solution online. Just a few notes:

  1. 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.

  2. 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.

arimuti commented 4 years ago

image

The output is None

morinigiu commented 4 years ago
Schermata 2019-12-08 alle 15 53 24
giuliamanganelli commented 4 years ago

Screen Shot 2019-12-08 at 15 54 10