Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lecture "Organising information: unordered structures", exercise 2 #22

Open
essepuntato opened this issue Nov 8, 2020 · 19 comments
Open
Labels

Comments

@essepuntato
Copy link
Contributor

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

@IlaRoss
Copy link

IlaRoss commented Nov 9, 2020

my_set = set({'Frodo', 'Sam', 'Pippin', 'Merry', 'Bilbo'})
after ​my_set.remove('Bilbo') --> my_set = set({'Frodo', 'Sam', 'Pippin', 'Merry'})
after ​my_set.add('Galadriel') --> my_set = set({'Frodo', 'Sam', 'Pippin', 'Merry', 'Galadriel'})
after ​my_set.update(set({'Saruman', 'Frodo', 'Gandalf'})) --> my_set = set({'Frodo', 'Galadriel', 'Gandalf', 'Sam', 'Saruman', 'Pippin', 'Merry'})

@penelopelask
Copy link

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

#will print

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

@gabrielefiorenza
Copy link

The status of my_set will be:
my_set = {'Pippin', 'Sam', 'Galadriel', 'Gandalf', 'Frodo', 'Merry', 'Saruman'}

@ChiaraCati
Copy link

lotr_set = set({'Frodo', 'Sam', 'Merry', 'Pippin', 'Bilbo'})
lotr_set.remove('Bilbo')
# current state {'Merry', 'Frodo', 'Sam', 'Pippin'}

lotr_set.add('Galadriel')
# current state {'Sam', 'Pippin', 'Galadriel', 'Merry', 'Frodo'}

more_set = set()
lotr_set.add('Saruman')
lotr_set.add('Frodo')
lotr_set.add('Gandalf')
# create new set to add

lotr_set.update(more_set)
print(lotr_set)
# output {'Sam', 'Galadriel', 'Pippin', 'Frodo', 'Saruman', 'Merry', 'Gandalf'} 
# 'Frodo' is not repeated because sets do not allow repetitions

@dbrembilla
Copy link

After my_set.remove('Bilbo') the set contains 'Merry', 'Frodo', 'Pippin', 'Sam'
after my_set.add('Galadriel') the set contains 'Merry', 'Frodo', 'Pippin', 'Sam', 'Galadriel'
after my_set.update(set({"Saruman", "Frodo", "Gandalf"})) the set contains 'Merry', 'Frodo', 'Pippin', 'Sam', 'Galadriel', 'Saruman', 'Gandalf'

@edoardodalborgo
Copy link

my_set = {'Bilbo', 'Frodo', 'Sam', 'Pippin', 'Merry'}
my_set.remove('Bilbo')
#after this statement the string "Bilbo" is removed from the set
my_set.add("Galadriel")
#after this statement the string "Galadriel" is add to the list, but we don't know in what position
my_set.update({"Saruman", "Frodo", "Gandalf"})
#after this statement all the elements of a new set, if they aren't already in my_set,
#are included in, but we don't know in what order the elements are added

@GiuliaMenna
Copy link

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(set({"Saruman", "Frodo", "Gandalf"}))

#Output = "Saruman" ,"Galadriel", "Frodo", "Sam", "Merry","Gandalf", "Pippin"

@giorgiasampo
Copy link

Starting Set:
my_set= set()
my_set.add("Bilbo")
my_set.add("Frodo")
my_set.add("Sam")
my_set.add("Merry")
my_set.add("Pippin")

​my_set.remove("Bilbo"): my_set = {'Pippin', 'Sam', 'Frodo', 'Merry'}
​my_set.add("Galadriel"):my_set = {'Frodo', 'Merry', 'Pippin', 'Sam', 'Galadriel'}
​my_set.update(set({"Saruman", "Frodo", "Gandalf"})): my_set = {'Frodo', 'Saruman', 'Merry', 'Pippin', 'Sam', 'Galadriel', 'Gandalf'}

@LuisAmmi
Copy link

char_set =  {'Frodo', 'Merry', 'Sam', 'Bilbo', 'Pippin'}

char_set.remove("Bilbo") --> char_set =  {'Sam', 'Frodo', 'Merry', 'Pippin'}
char_set.add("Galadriel") --> char_set =  {'Sam', 'Frodo', 'Galadriel', 'Merry', 'Pippin'}
char_set.update(set({"Saruman", "Frodo", "Gandalf"})). --> char_set =  {'Sam', 'Galadriel', 'Saruman', 'Frodo', 'Merry', 'Gandalf', 'Pippin'}
# The 'update' operation excludes any duplicate item, this is the reason why 'Frodo' appears only once. 

@fcagnola
Copy link

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

lotr_set.remove("Bilbo") # "Bilbo will be removed from the set
# lotr_set contains: {'Pippin', 'Merry', 'Sam', 'Frodo'}

lotr_set.add("Galadriel") # "Galadriel" will be added to the set
# lotr_set contains: {'Pippin', 'Merry', 'Sam', 'Frodo', 'Galadriel'}

lotr_set.update(set({"Saruman", "Frodo", "Gandalf"})) # specified set will be added to the variable lotr_set, with the exception of duplicate items
# lotr_set contains {'Pippin', 'Merry', 'Sam', 'Frodo', 'Galadriel', 'Saruman', 'Gandalf'}

@SofiBar
Copy link

SofiBar commented Nov 10, 2020

my_set.remove("Bilbo") # my_set = ({"Frodo", "Sam", "Pippin", "Mary"}) my_set.add("Galadriel") # my_set = ({"Frodo", "Galadriel", "Sam", "Pippin", "Mary"}) my_set.update(set({"Saruman", "Frodo", "Gandalf"})) # my_set = ({"Frodo", "Sam", "Pippin", "Mary", "Saruman", "Gandalf"})

@yunglong28
Copy link

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

@SarahTew
Copy link

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

When I ran these all in PythonTutor it would switch the order of the objects. Sets are unordered so it doesn't really matter I just wondering how it decides where to put them.

@vanessabonanno
Copy link

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 element "Bilbo" is removed from my_set
# output: {'Sam', 'Frodo', 'Pippin', 'Merry'}

my_set.add("Galadriel") 
# the element "Galadriel" is added to my_set
# output: {'Sam', 'Galadriel', 'Frodo', 'Pippin', 'Merry'}

my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
# a second set is added to "my_set"
# output: {'Gandalf', 'Saruman', 'Sam', 'Frodo', 'Pippin', 'Merry', 'Galadriel'}
# "Frodo" is not added because it is already present in my_set

@Camillaneri
Copy link

`my_set = set(({'Frodo', 'Sam', 'Pippin', 'Merry', 'Bilbo'}))
my_other_set = set(({"Saruman", "Frodo", "Gandalf"}))

my_set.remove("Bilbo")

#{'Sam', 'Frodo', 'Merry', 'Pippin'}

my_set.add("Galadriel")

#{'Sam', 'Galadriel', 'Frodo', 'Merry', 'Pippin'}

my_set.update(my_other_set)

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

@AlessandraFa
Copy link

my_set = set({'Frodo', 'Sam', 'Pippin', 'Merry', 'Bilbo'})
my_set.remove("Bilbo")   # removes item "Bilbo" from set"
my_set.add("Galadriel")  # adds "Galadriel" to the set
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))  # adds elements from another set to my_set
                               
# Final status: {'Pippin', 'Sam', 'Frodo', 'Saruman', 'Merry', 'Gandalf', 'Galadriel'} - these are the elements contained in the set. 
# They are printed in random order because the order of the items in a set is not relevant. Since sets don't allow multiple 
# identical items, the item "Frodo" appears just once.

@LauOcchipinti
Copy link

my_set = set()
my_set.add("Bilbo")
my_set.add("Frodo")
my_set.add("Pippin")
my_set.add("Marry")
print(my_set)
{'Pippin', 'Marry', 'Frodo', 'Bilbo'}
my_set.remove("Bilbo")
print(my_set)
{'Pippin', 'Marry', 'Frodo'}
my_set.add("Galadriel")
print (my_set)
{'Galadriel', 'Pippin', 'Marry', 'Frodo'}
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
print(my_set)
{'Galadriel', 'Pippin', 'Marry', 'Frodo', 'Gandalf', 'Saruman'}

@AleRosae
Copy link

LOTR_set = set ()
LOTR_set.add("Bilbo")
LOTR_set.add("Frodo")
LOTR_set.add("Sam")
LOTR_set.add("Pippin")
LOTR_set.add("Merry")

print(LOTR_set) #returns {'Bilbo', 'Pippin', 'Frodo', 'Merry', 'Sam'}

LOTR_set.remove("Bilbo") #Python will remove the item "Bilbo" 

print(LOTR_set) #returns {'Pippin', 'Frodo', 'Merry', 'Sam'}

LOTR_set.add("Galadriel") #Python will add the item "Galadriel" to the set

print(LOTR_set) #returns {'Galadriel', 'Pippin', 'Frodo', 'Merry', 'Sam'}

LOTR_set.update((set({"Saruman", "Frodo", "Gandalf"}))) #Python will update the set by includin another set which includes items "Saruman", "Frodo" and "Gandalf"

print(LOTR_set) #returns {'Galadriel', 'Gandalf', 'Saruman', 'Pippin', 'Frodo', 'Merry', 'Sam'}

@SusannaPinotti
Copy link

lotr_set=set()
lotr_set.add("Bilbo")
lotr_set.add("Frodo")
lotr_set.add("Sam")
lotr_set.add("Pippin")
lotr_set.add("Merry")

lotr_set.remove("Bilbo")
lotr_set.add("Galadriel")
lotr_set.update( set({"Saruman", "Frodo", "Gandalf"}))

#the element Frodo in the added set will replace the same value in lotr_set, because it is not possible to have the same #value twice in a set
lotr_set = {"Pippin", "Merry", "Frodo", "Galadriel", "Gandalf", "Sam", "Saruman"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests