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 "Organising information: unordered structures", exercise 2 #22

Open essepuntato opened 1 year ago

essepuntato commented 1 year ago

Consider the set created in the first exercise, stored in the variable my_set. Describe the status of ​my_set after the execution of each of the following operations: ​my_set.remove("Bilbo"), ​my_set.add("Galadriel"), ​my_set.update(set({"Saruman", "Frodo", "Gandalf"})).

n1kg0r commented 1 year ago
my_set = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"} 

my_set.remove("Bilbo")
# {'Frodo', 'Sam', 'Merry', 'Pippin'}  
# "Bilbo" was removed from the set

my_set.add("Galadriel")
# {'Merry', 'Galadriel', 'Sam', 'Frodo', 'Pippin'}
# "Galadriel" was added to the set

my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
# {'Pippin', 'Sam', 'Saruman', 'Merry', 'Frodo', 'Gandalf', 'Galadriel'}
# my_set was updated by adding new elements from the new set (namely 'Saruman', 'Gandalf')
# all my_set's elements 'Merry', 'Galadriel', 'Sam', 'Frodo', 'Pippin' were left untouched
lvcasmariani commented 1 year ago

my_set = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}

my_set.remove("Bilbo")

"Bilbo" was removed from the set

{"Pippin", "Frodo", "Merry", "Sam"}

my_set.add("Galadriel")

"Galadriel" was added to the set

{"Pippin", "Galadriel", "Frodo", "Merry", "Sam"}

my_set.update(set({"Saruman", "Frodo", "Gandalf"}))

"Saruman" and "Gandalf" were added to the set

my_set is updated by adding new elements from new set

"Pippin", "Galadriel", "Frodo", "Merry", "Sam" are untouched

{"Frodo", "Gandalf", "Galadriel", "Sam", "Saruman", "Pippin", Merry"}

print(my_set)

delete4ever commented 1 year ago
my_set= {"Bilbo","Frodo","Sam","Pippin","Merry"}
my_set.remove("Bilbo")
# "Bilbo" was removed from the set. 
#{"Frodo","Sam","Pippin","Merry"}
my_set.add("Galadriel")
# "Galadriel" was added to the set.
#{"Frodo","Sam","Pippin","Merry","Galadriel"}
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
# "Saruman", "Frodo" and "Gandalf" were added from a new set to original set.
#{'Pippin', 'Gandalf', 'Galadriel', 'Merry', 'Frodo', 'Sam', 'Saruman'}
EricaAndreose commented 1 year ago
my_set = set()
my_set.add("Bilbo")
my_set.add("Frodo")
my_set.add("Sam")
my_set.add("Pippin")
my_set.add("Merry")

my_set.remove("Bilbo")
# the item "Bilbo" is not on the set anymore
my_set.add("Galadriel")
# the new item "Galadrield" id added to the set
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
# a new set is added to the previous one
# "Saruman" and "Gandalf" are new items
# item "Frodo" is already inside my_set and won't be duplicated
print(my_set)

# Terminal run: {'Galadriel', 'Merry', 'Saruman', 'Gandalf', 'Frodo', 'Pippin', 'Sam'}
GaiaOrtona commented 1 year ago

`my_second_set.remove("Bilbo") print(my_second_set)

the result is {"Pippin", "Frodo", "Sam", "Merry"}

my_second_set.add("Galadriel") print(my_second_set)

the result is {"Frodo", "Sam", "Merry", "Galadriel", "Pippin"}

my_second_set.update(set({"Saruman", "Frodo", "Gandalf"})) print(my_second_set)

the result is {"Sam", "Gandalf", "Saruman", "Merry", "Galadriel", "Pippin", "Frodo"}`

giorgiacrosilla commented 1 year ago
my_set = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}
my_set.remove("Bilbo")
#"Bilbo" is removed from the set
# terminal: {"Sam", "Merry", "Pippin", "Frodo"}
my_set.add("Galadriel")
#"Galadriel" is added to the set
# terminal: {"Galadriel", "Sam", "Pippin", "Frodo", "Merry"}
my_set.update(set(("Saruman", "Frodo", "Gandalf")))
#adding elements included in another set
print(my_set)
#terminal: {'Merry', 'Gandalf', 'Frodo', 'Galadriel', 'Saruman', 'Sam', 'Pippin'}
mjavadf commented 1 year ago

by calling my_set.remove("Bilbo") the "Bilbo"'s item will be removed from my_set by calling my_set.add("Galadriel") the "Galadriel" will be added to my_set by calling my_set.update(set({"Saruman", "Frodo", "Gandalf"})) items of given set will be added to my_set

image

tiglio95 commented 1 year ago

elements = set() elements.add("Bilbo") elements.add("Frodo") elements.add("Sam") elements.add("Pippin") elements.add("Merry") elements.remove("Bilbo") elements.add("Galadriel") elements.update(set({"Saruman", "Frodo", "Gandalf"})) print(elements)

output: {'Frodo', 'Saruman', 'Pippin', 'Galadriel', 'Merry', 'Sam', 'Gandalf'}

lucia1299 commented 1 year ago

my_set = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}

my_set.remove ("Bilbo")
print(my_set)
#Console output
{"Frodo", "Sam", "Pippin", "Merry"}
#"Bilbo" was removed from my_set 

my_set.add("Galandriel")
print(my_set)
#Console output
{"Frodo", "Sam", "Pippin", "Merry", "Galandriel"}
#"Galandriel" was added to my_set

my_set.update({"Saruman", "Frodo", "Gandalf"})
print(my_set)
#Console output 
{"Frodo", "Sam", "Merry", "Pippin", "Galandriel", "Saruman", "Gandalf"}
#"Saruman" and "Gandalf" were added to my_set, "Frodo" was not added a second time

#In general, elements are returned in a different order every time
ranacoskun commented 1 year ago
my_set = set(("Bilbo","Frodo","Sam","Merry","Pippin"))
my_set.remove("Bilbo")
# {'Pippin', 'Frodo', 'Sam', 'Merry'}

my_set.add("Galadriel")
#{'Pippin', 'Frodo', 'Sam', 'Galadriel', 'Merry'}

my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
#{'Pippin', 'Gandalf', 'Frodo', 'Sam', 'Galadriel', 'Saruman', 'Merry'}
matteo-guenci commented 1 year ago
my_set = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"} 

my_set.remove("Bilbo")
#{'Frodo', 'Sam', 'Merry', 'Pippin'} == Bilbo has been removed 

my_set.add("Galadriel")
# {'Merry', 'Galadriel', 'Sam', 'Frodo', 'Pippin'} == Galadriel has been added 

my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
# {'Pippin', 'Sam', 'Saruman', 'Merry', 'Frodo', 'Gandalf', 'Galadriel'} == my_set has been updated, Saruman and Gandalf were added to the set while Frodo wasn't because he was already in 
corrado877 commented 1 year ago
my_first_set= set()
my_first_set.add ("Bilbo")
my_first_set.add ("Frodo")
my_first_set.add ("Sam")
my_first_set.add ("Pippin")
my_first_set.add ("Merry")
my_first_set.remove ("Bilbo")
my_first_set.add ("Galadriel")
my_first_set.update(set({"Saruman", "Frodo", "Gandalf"}))

print (my_first_set)
{'Saruman', 'Pippin', 'Sam', 'Merry', 'Galadriel', 'Gandalf', 'Frodo'}
SalvatoreDiMarzo commented 1 year ago
my_set ={"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}
my_set.remove("Bilbo")
my_set.add("Galadriel")
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
print(my_set)

{'Pippin', 'Sam', 'Merry', 'Gandalf', 'Galadriel', 'Frodo', 'Saruman'}

ChiaraParravicini commented 1 year ago
my_set = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}
my_set.remove("Bilbo")
# the item "Bilbo" has been removed
my_set.add("Galadriel")
# the item "Galadriel" has been added
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
# of the elements of the new set, only "Saruman" and "Gandalf" have been added to the current set, since "Frodo" was already present
print(my_set)
# Now the set contains:
# set({"Frodo", "Sam", "Pippin", "Galadriel", "Gandalf", "Merry", "Saruman"}) 
eugeniavd commented 1 year ago

my_set = set()

new empty set is created

my_set.add("Bilbo") my_set.add("Frodo") my_set.add("Pippin") my_set.add("Merry")

four elements are added to the my_set

my_set.remove("Bilbo")

Bilbo was removed from the my_set

my_set.add("Galadriel")

Galadriel was added to the my_set

my_set.update(set({"Saruman", "Frodo", "Gandalf"}))

all the elements of the set except Frodo are added to the my_set

Frodo isn't added because he is already there

checking

print(my_set)

{'Saruman', 'Galadriel', 'Pippin', 'Merry', 'Frodo', 'Gandalf'}

alka2696 commented 1 year ago
my_set = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}
my_set.remove("Bilbo")
#"Bilbo" is removed from the set
#  {"Sam", "Merry", "Pippin", "Frodo"}

my_set.add("Galadriel")
#"Galadriel" is added to the set
#  {"Galadriel", "Sam", "Pippin", "Frodo", "Merry"}

my_set.update(set(("Saruman", "Frodo", "Gandalf")))
#adding elements included in another set
print(my_set)
# {'Merry', 'Gandalf', 'Frodo', 'Galadriel', 'Saruman', 'Sam', 'Pippin'}