comp-think/2022-2023

Lecture "Organising information: unordered structures", exercise 2

essepuntato opened this issue · 16 comments

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

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

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)

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'}
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'}

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

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'}

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

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'}

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
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'}
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 
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'}
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'}

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

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'}

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'}