comp-think / 2022-2023

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. 2022/2023).
17 stars 5 forks source link

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

Open essepuntato opened 1 year ago

essepuntato commented 1 year 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.

tiglio95 commented 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)

I will get this enumeration [(0, "Coraline"), (1, "American Gods"), (2, "The Graveyard Book"), (3, "Good Omwens"), (4, "Neverwhere")]

#

Iteration 1

position = 0

item = "Coraline"

item == "The Sandman" is False

Continue to the next iteration

#

Iteration 2

position = 1

item = "American Gods"

item == "The Sandman" is False

Continue to the next iteration

Iteration 3

position = 2

item = "The Graveyard Book"

item =

Continue to the next iteration

#

Iteration 4

position = 3

item = "Good Omens"

item = "The Sandman" is False

Continue to the next iteration

#

Iteration 5

position = 4

item = "Neverwhere"

item = "The Sandman" is False

Return None

lucia1299 commented 1 year ago

`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`

n1kg0r commented 1 year ago
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
SalvatoreDiMarzo commented 1 year ago

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

delete4ever commented 1 year ago
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
alka2696 commented 1 year ago

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

EricaAndreose commented 1 year ago
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"
matteo-guenci commented 1 year ago

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")

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 == The Sandman is False

Continue to the next iteration

#

Iteration 2

position = 1

item = "American Gods"

item == The Sandman is False

Continue to the next iteration

#

Iteration 3

position = 2

item = "The Graveyard Book"

item == The Sandman is False

Iteration 4

position = 3

item = "Good Omens"

item == The Sandman is False

Iteration 5

position = 4

item = "Neverwhere"

item == The Sandman is False

return None

matteo-guenci commented 1 year ago

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")

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 == The Sandman is False

Continue to the next iteration

#

Iteration 2

position = 1

item = "American Gods"

item == The Sandman is False

Continue to the next iteration

#

Iteration 3

position = 2

item = "The Graveyard Book"

item == The Sandman is False

Iteration 4

position = 3

item = "Good Omens"

item == The Sandman is False

Iteration 5

position = 4

item = "Neverwhere"

item == The Sandman is False

return None

ChiaraParravicini commented 1 year ago
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
giorgiacrosilla commented 1 year ago
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 
GaiaOrtona commented 1 year ago

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")])

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

ranacoskun commented 1 year ago
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.
corrado877 commented 1 year ago

Exercise 01 slide 06.odt

irematmar commented 1 year ago

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

essepuntato commented 1 year ago

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
```