2018-2019 icon indicating copy to clipboard operation
2018-2019 copied to clipboard

Lecture "Organising information: unordered structures", exercise 2

Open essepuntato opened this issue 6 years ago • 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"})).

essepuntato avatar Nov 28 '18 07:11 essepuntato

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"

hizclick avatar Nov 28 '18 10:11 hizclick

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

friendlynihilist avatar Nov 28 '18 12:11 friendlynihilist

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

andreamust avatar Nov 28 '18 12:11 andreamust

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

dersuchendee avatar Nov 28 '18 12:11 dersuchendee

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

bluebell94 avatar Nov 28 '18 13:11 bluebell94

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

beccadelbens avatar Nov 28 '18 15:11 beccadelbens

# 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

EleonoraPeruch avatar Nov 28 '18 16:11 EleonoraPeruch

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

simayguzel avatar Nov 28 '18 18:11 simayguzel

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

leticiasandra avatar Nov 28 '18 19:11 leticiasandra

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

delfimpandiani avatar Nov 28 '18 21:11 delfimpandiani

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

MattiaSpadoni avatar Nov 29 '18 14:11 MattiaSpadoni

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

SeverinJB avatar Nov 29 '18 14:11 SeverinJB

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

ilsamoano avatar Nov 29 '18 15:11 ilsamoano

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)

Totaro1996 avatar Nov 29 '18 15:11 Totaro1996

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

lisasiurina avatar Nov 29 '18 20:11 lisasiurina

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

tceron avatar Nov 30 '18 07:11 tceron

esercizio 2 Last operation doesen't add "Frodo" because it's already in the set.

MilenaCorbellini avatar Nov 30 '18 14:11 MilenaCorbellini

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

DavideApolloni avatar Dec 01 '18 11:12 DavideApolloni

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

Saraa04 avatar Dec 01 '18 17:12 Saraa04