Lecture "Organising information: unordered structures", exercise 2
Opened this issue · 19 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.remove("Bilbo") #remove bilbo from the set
my_set.add("Galadriel") #adds Galadriel to the set
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
#updates my_set by adding "Saruman" and "Gandalf"
Considering our starting hobbits({"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}):
hobbits = set()
hobbits.add("Bilbo")
hobbits.add("Frodo")
hobbits.add("Sam")
hobbits.add("Pippin")
hobbits.add("Merry")
We execute:
hobbits.remove("Bilbo") #"Bilbo" element is removed. our set contains now ({"Frodo", "Sam", "Pippin", "Merry"})
hobbits.add("Galadriel") #"Galadriel" element is added to our "hobbits" set
hobbits.update(lotr({"Saruman", "Frodo", "Gandalf"})) #it updates the first list with the elements contained in the second set. elements in set are not repeatable, so "Frodo" will not be added.
The final set is:
hobbits({"Frodo", "Sam", "Pippin", "Merry", "Galadriel", "Saruman", "Gandalf"})
my_set = set() # this creates a new set
my_set.add("Bilbo") # these 5 lines add five names
my_set.add("Frodo")
my_set.add("Sam")
my_set.add("Pippin")
my_set.add("Merry")
print(my_set) # the output is {'Bilbo', 'Frodo', 'Pippin', 'Merry', 'Sam'}
#exercise2 starts
my_set.remove("Bilbo") # this deletes an element
print(my_set) # the result is {'Sam', 'Merry', 'Pippin', 'Frodo'}
my_set.add("Galadriel") # this adds a new element
print(my_set) # the result is {'Sam', 'Merry', 'Galadriel', 'Pippin', 'Frodo'}
my_set.update(set({"Saruman", "Frodo", "Gandalf"})) # this adds "Saruman" and "Gandalf" but not "Frodo" because this element is already in the list
print(my_set) # the result is: {'Saruman', 'Galadriel', 'Frodo', 'Gandalf', 'Pippin', 'Merry', 'Sam'}
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"})).
set_tolkien= ()
set_tolkien.add("Bilbo")
set_tolkien.add("Frodo")
set_tolkien.add("Sam")
set_tolkien.add("Pippin")
set_tolkien.add("Merry")
Current status of set_tolkien: ({"Bilbo", "Frodo", "Sam", "Pippin", "Merry"})
set_tolkien.remove("Bilbo")
Current status of set_tolkien: ({"Frodo", "Sam", "Pippin", "Merry"})
set_tolkien.add("Galadriel")
Current status of set_tolkien: ({"Frodo", "Sam", "Pippin", "Merry", "Galadriel"})
set_tolkien.update({"Saruman", "Frodo", "Gandalf"}))
Current status of set_tolkien: ({"Frodo", "Sam", "Pippin", "Merry", "Galadriel", "Saruman", "Gandalf"})
initial stage of the set protagonists= ({"Bilbo", "Frodo", "Sam", "Pippin", "Merry"})
protagonists.remove("Bilbo")- will remove "Bilbo"
current status of the set - ({"Frodo", "Sam", "Pippin", "Merry"})
protagonists.add("Galadriel")- will add "Galadriel"
current status of the set -({"Frodo", "Sam", "Galadriel", "Pippin", "Merry"})
protagonists.update(set({["Saruman", "Frodo", "Gandalf"})) -will add all except "Frodo", as the set doesn't accept repeatability of items
final status of the set ({"Frodo", "Saruman", "Sam", "Gandalf", "Galadriel", "Pippin", "Merry"})
mag_set = set()
mag_set.add("Saruman")
mag_set.add("Frodo")
mag_set.add("Gandalf")
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")
my_set.add("Galadriel")
my_set.update(mag_set)
#my_set({"Pippin", "Gandalf", "Sam", "Merry", "Frodo", "Saruman", "Galadriel"})
# this is only one possible combination
# set({"Bilbo", "Merry", "Sam","Frodo","Pippin"})
my_set.remove("Bilbo") # the element "Bilbo" will be removed from the set
my_set.add("Galadriel") # add the element "Galadriel" to the set
my_set.update(set({"Saruman", "Frodo", "Gandalf"})) # "Frodo" will not be added
# as it is already included in the set
print(my_set)
# set({"Gandalf", "Merry", "Sam", "Galadriel", "Saruman", "Frodo", "Pippin"})
# the elements can be combined in a different order
# I already have the set that I have done in the first exercise which is {'Sam', 'Bilbo', 'Frodo', 'Merry', 'Pippin'}
hobbitset.remove("Bilbo")
hobbitset.add("Galadriel")
hobbitset.update(set({"Saruman","Frodo","Gandalf"})) #since there is no need to sort the list, we don't have to do an extra execution.
print(hobbitset)
outcome = {'Sam', 'Gandalf', 'Frodo', 'Merry', 'Saruman', 'Galadriel', 'Pippin'}
my_name_set = set()
my_name_set.add("Bilbo")
my_name_set.add("Frodo")
my_name_set.add("Sam")
my_name_set.add("Pippin")
my_name_set.add("Merry")
my_name_set.remove("Bilbo"),
my_name_set.add("Galadriel"),
my_name_set.update(set({"Saruman", "Frodo", "Gandalf"}))
print(my_name_set)
my_name_set = ("Frodo", "Pippin", "Gandalf", "Sam", "Merry", "Saruman", "Galadriel")
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({ "Bilbo", "Frodo", "Sam", "Pippin", "Merry" })
my_set.remove("Bilbo")
# my_set({ "Frodo", "Sam", "Pippin", "Merry" })
my_set.add("Galadriel")
# my_set({ "Frodo", "Sam", "Pippin", "Merry", "Galadriel" })
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
# my_set({"Frodo", "Sam", "Pippin", "Merry", "Galadriel", "Saruman", "Gandalf"})
#Theoden: so, it begins
sauron._set({"Bilbo", "Frodo", "Pippin", "Sam", "Merry"})
Actions:
my_set.remove("Bilbo"), my_set.add("Galadriel"), my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
#first step:
sauron_set.remove("Bilbo")
sauron._set({"Frodo", "Pippin", "Sam", "Merry"})
#second step
sauron_set.add("Galadriel")
sauron._set({"Frodo", "Pippin", "Sam", "Merry", "Galadriel"})
#third step
sauron_set.update(set({"Saruman", "Frodo", "Gandalf"}))
sauron._set({"Frodo", "Pippin", "Sam", "Merry", "Galadriel", "Saruman", "Frodo", "Gandalf"})
#end, now go to defend Helm's Deep.
1 set_der_besten_hobbits = set(["Bilbo","Frodo","Sam","Pippin","Merry"])
2 set_der_besten_hobbits.remove("Bilbo”)
3 set_der_besten_hobbits.add("Galadriel")
4 set_der_besten_hobbits.update(set({"Saruman", "Frodo", "Gandalf"}))
In the second line, "Bilbo" dies on set. The set now persists of {'Frodo', 'Sam', ‘Merry', ‘Pippin'}.
In the third line, “Galadriel" fills the gap which "Bilbo" left behind. She is now the first woman on set: {'Frodo', 'Sam', 'Merry', 'Galadriel', ‘Pippin'}.
In the fourth line, two old men and a Frodo double join the set. Seven different characters are now on set: {‘Saruman’, 'Sam', 'Frodo', 'Merry', 'Galadriel', 'Pippin', 'Gandalf'}.
mordor_set = set() #used set() to create a new set
mordor_set.add("Bilbo") #added new element to the set
mordor_set.add("Frodo") #added new element to the set
mordor_set.add("Sam") #added new element to the set
mordor_set.add("Pippin") #added new element to the set
mordor_set.add("Merry") #added new element to the set
print(mordor_set)
#{'Frodo', 'Pippin', 'Merry', 'Bilbo', 'Sam'}
mordor_set.remove("Bilbo") #method remove used to remove string "Bilbo"
mordor_set.add("Galadriel") #method add used to add string "Galadriel"
mordor_set.update(set({"Saruman", "Frodo", "Gandalf"})) #method update used to add to mordor_set new elements from a second set
print(mordor_set)
#{'Gandalf', 'Frodo', 'Pippin', 'Merry', 'Galadriel', 'Saruman', 'Sam'}
hobbitset=set (["Frodo","Bilbo","Sam","Pippin","Merry"])
hobbitset.remove("Bilbo") #it removes the string "Bilbo"
hobbitset.add("Galadriel") #it adds the string "Galadriel"
new_set=set(["Saruman","Frodo","Gandalf"]) #it creates another set with new elements
hobbitset.update(new_set) #it is used for adding all the elements included in new_set
print(hobbitset)
set(['Bilbo', 'Merry', 'Pippin', 'Sam', 'Frodo'])
my_set = set()
my_set.add("Bilbo")
my_set.add("Frodo")
my_set.add("Sam")
my_set.add("Pippin")
my_set.add("Pippin")
my_set.add("Merry")
my_set.remove("Bilbo") #this removes 'Bilbo" from the set
my_set.add("Galadriel") #this adds "Galadriel" to the set
my_set.update(set({"Saruman", "Frodo", "Gandalf"})) #adds all the elements included in to the my_set accept for "Frodo" which has been already added to the set
print (my_set)
output set(['Pippin', 'Galadriel', 'Sam', 'Frodo', 'Merry', 'Gandalf', 'Saruman'])
my_set.remove("Bilbo"), my_set.add("Galadriel"), my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
my_set{'Bilbo', 'Merry', 'Pippin', 'Sam', 'Frodo'}
my_set.remove('Bilbo') #removes the item 'Bilbo'
my_set.add('Galadriel') #adds the item 'Galafriel'
my_set.update(set({'Saruman', 'Frodo', 'Gandalf'})) #it adds them items 'Saruman, and 'Gandalf' to the list, it doesn't add 'Frodo' because it was already present in the list.
Output: my_set{'Merry', 'Pippin', 'Sam', 'Frodo', 'Galadriel', 'Saruman', 'Gandalf'}
my_set=({"Bilbo", "Frodo", "Sam", "Pippin", "Merry"})
my_set.remove("Bilbo")
#"Bilbo" wears the one ring and disappears from the set
output: my_set({"Frodo", "Sam", "Pippin", "Merry"})
my_set.add("Galadriel")
#"Galadriel" joins the set while she is looking for Aragorn
output: my_set({"Frodo", "Sam", "Galadriel, "Pippin", "Merry"})
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
#"Saruman" and "Gandalf", after having enjoyed some fireworks, join the set. "Frodo" is not added, as he is already present
output: my_set({"Gandalf", "Saruman, "Frodo", "Sam", "Galadriel, "Pippin", "Merry"})
Considering the first exercise:
the_hobbits = set()
the_hobbits.add("Bilbo")
the_hobbits.add("Frodo")
the_hobbits.add("Sam")
the_hobbits.add("Pippin")
the_hobbits.add("Merry")
print(the_hobbits)
exercise 2: my_set.remove("Bilbo"), my_set.add("Galadriel"), my_set.update(set({"Saruman", "Frodo", "Gandalf"})).
the_hobbits.remove("Bilbo") # this removes "Bilbo" from the set
# the_hobbits set became:
# set ({"Frodo", "Sam", "Pippin", "Merry"})
the_hobbits.add("Galadriel") # this adds "Galadriel" to the set
# now the_hobbits set contains:
# set ({"Frodo", "Sam", "Pippin", "Merry", "Galadriel"})
the_hobbits.update(set({"Saruman", "Frodo", "Gandalf"}))
# it updates the_hobbits set
# it does not add the element "Frodo" to the set because it was already included
print(the_hobbits)
Output: set({'Galadriel', 'Gandalf', 'Pippin', 'Frodo', 'Sam', 'Saruman', 'Merry'})