comp-think / 2020-2021

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. 2020/2021).
14 stars 9 forks source link

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

Open essepuntato opened 4 years ago

essepuntato commented 4 years 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"})).

IlaRoss commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

ChiaraCati commented 4 years ago
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 commented 4 years ago

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 commented 4 years ago
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 commented 4 years 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")
my_set.add("Galadriel")
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))

#Output = "Saruman" ,"Galadriel", "Frodo", "Sam", "Merry","Gandalf", "Pippin"
giorgiasampo commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago
# 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 commented 4 years ago

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 commented 4 years ago

{'Gandalf', 'Frodo', 'Saruman', 'Sam', 'Merry', 'Pippin', 'Galadriel'}`
SarahTew commented 4 years ago
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 commented 4 years 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 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 commented 4 years ago

`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 commented 4 years ago
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 commented 4 years ago

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 commented 4 years ago
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 commented 4 years ago

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