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: ordered structures", exercise 2 #14

Open essepuntato opened 3 years ago

essepuntato commented 3 years ago

Consider to have a stack obtained by processing, one by one, the elements included in the list of the first exercise, i.e. my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]). Describe the status of my_stack after the execution of each of the following operations: my_stack.pop(), my_stack.pop(), my_stack.append("Voldemort").

SarahTew commented 3 years ago
  1. my_stack = deque(["Draco", "Harry", "Hermione", "Ron"])
  2. my_stack = deque(["Draco", "Harry", "Hermione"])
  3. my_stack = deque(["Draco", "Harry", "Hermione", "Voldemort"])
ChiaraCati commented 3 years ago

my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) my_stack.pop() returns: Draco, Harry, Hermione, Ron my_stack.pop() returns: Draco, Harry, Hermione my_stack.append( "Voldemort") returns: Draco, Harry, Hermione, Voldemort

margheparla commented 3 years ago

from collections import deque

my_stack = deque(["Draco", "Harry","Hermione", "Ron", "Severus"]) my_stack.pop() my_stack.pop() my_stack.append("Voldemort") print(my_stack)

yunglong28 commented 3 years ago

from _collections import deque # This imports deque my_stack = deque() # This creates a new stack

my_stack.append("Draco") # Now we add the elements my_stack.append("Harry") my_stack.append("Hermione") my_stack.append("Ron") my_stack.append("Severus") deque(['Draco', 'Harry', 'Hermione', 'Ron', 'Severus'])

my_stack.pop() # Now we execute the exercise my_stack.pop() my_stack.append("Voldemort") print(my_stack) deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])

AleRosae commented 3 years ago

from _collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop() # returns deque(['Draco', 'Harry', 'Hermione', 'Ron'])
my_stack.pop() # returns deque(['Draco', 'Harry', 'Hermione'])
my_stack.append("Voldemort") # returns deque(['Draco', 'Harry', 'Hermione', 'Voldermort'])
dbrembilla commented 3 years ago

my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]). After my_stack.pop(), my_stack would contain "Draco", "Harry", "Hermione", "Ron" After again my_stack.pop() my_stack would contain "Draco", "Harry", "Hermione" After my_stack.append("Voldemort"), my_stack would contain "Draco", "Harry", "Hermione", "Voldemort"

edoardodalborgo commented 3 years ago
from collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop() #it removes the last string of the stack ("Severus") and return "Severus"
my_stack.pop() #it removes the last string of the stack ("Ron") and return "Ron"
my_stack.append("Voldemort") #it adds the string "Voldemort" at the end of the stack
print(my_stack) #it prints deque["Draco", "Harry", "Hermione", "Voldemort"]
laurentfintoni commented 3 years ago

status 1 - deque(["Ron", "Hermione", "Harry", "Draco"]) status 2 - deque(["Hermione", "Harry", "Draco"]) status 3 - deque(["Voldemort", "Hermione", "Harry", "Draco"])

vanessabonanno commented 3 years ago

my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) my_stack.pop() #current status: deque(["Draco", "Harry", "Hermione", "Ron"]) my_stack.pop() #current status: deque(["Draco", "Harry", "Hermione"]) my_stack.append("Voldemort") #current status: deque(["Draco", "Harry", "Hermione", "Voldermort"])

AlessandraFa commented 3 years ago
from collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop()
print(my_stack)

my_stack.pop()
print(my_stack)

my_stack.append("Voldemort")
print(my_stack)

# Output:
deque(['Draco', 'Harry', 'Hermione', 'Ron']) # the last item of the deque ("Severus") is returned and removed
deque(['Draco', 'Harry', 'Hermione']) # after deleting "Severus", the last item of the deque is "Ron", which is again returned and removed from my_stack
deque(['Draco', 'Harry', 'Hermione', 'Voldemort']) # The string "Voldemort" is added at the end of the deque
LuisAmmi commented 3 years ago

my_stack = deque(["Draco","Harry","Hermione","Ron","Severus"]):
my_stack.pop() #the last item is removed and the algorithm returns deque(["Draco","Harry","Hermione","Ron"])
my_stack.pop() #returns deque(["Draco","Harry","Hermione"])
my_stack.append("Voldemort") #addition of this item
print(my_stack) #it will print ["Draco","Harry","Hermione","Voldemort"]
gabrielefiorenza commented 3 years ago
from collections import deque

my_stack = deque(["Draco", "Harry","Hermione", "Ron", "Severus"])
my_stack.pop()
my_stack.pop()
my_stack.append("Voldemort")

print(my_stack)

OUTPUT: deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])

penelopelask commented 3 years ago

from collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop()
my_stack.pop()
my_stack.append("Voldemort")
print(my_stack)

deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
giorgiasampo commented 3 years ago

Initial stack: my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])

1 - my_stack.pop(): my_stack = deque(["Draco", "Harry", "Hermione", "Ron"]) 2 - my_stack.pop(): my_stack = deque(["Draco", "Harry", "Hermione"]) 3 - my_stack.append("Voldemort"): my_stack = deque(["Draco", "Harry", "Hermione", "Voldemort"])

fcagnola commented 3 years ago

The stack will first eliminate the last element, then the second-to last, and then add the new one. Each step will modify the stack as follows: ["Draco", "Harry", "Hermione", "Ron"] then ["Draco", "Harry", "Hermione"] and finally ["Draco", "Harry", "Hermione", "Voldemort"]

samuelespotti commented 3 years ago

from collections import deque

my_stack = deque(["Draco", "Harry","Hermione", "Ron", "Severus"]) my_stack.pop() my_stack.pop() my_stack.append("Voldemort")

print(my_stack)

deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])

GiuliaMenna commented 3 years ago
from collections import deque

my_stack = deque(["Draco","Harry","Hermione","Ron","Severus"])
my_stack.pop()
my_stack.pop()
my_stack.append("Voldemort")

print(deque)

deque = (['Draco', 'Harry', 'Hermione', 'Voldemort'])

SusannaPinotti commented 3 years ago
from collections import deque

my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])

my_stack.pop()
print(my_stack)

my_stack.pop()
print(my_stack)

my_stack.append("voldemort")
print(my_stack)

deque(['Draco', 'Harry', 'Hermione', 'Ron'])
deque(['Draco', 'Harry', 'Hermione'])
deque(['Draco', 'Harry', 'Hermione', 'voldemort'])
alicebordignon commented 3 years ago

from collections import deque my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) my_stack.pop() print(my_stack) #(['Draco', 'Harry', 'Hermione', 'Ron']) my_stack.pop() print(my_stack) #(['Draco', 'Harry', 'Hermione']) my_stack.append("Voldemort") print(my_stack) #(['Draco', 'Harry', 'Hermione', 'Voldemort'])

DeniseBas commented 3 years ago

from collections import deque my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) my_stack.pop() my_stack.pop() my_stack.append("Voldemort") print(my_stack)

deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])

IlaRoss commented 3 years ago

my_stack: after my_stack.pop() --> my_stack = deque (["Draco", "Harry", "Hermione", "Ron"]) after my_stack.pop() --> my_stack = deque (["Draco", "Harry", "Hermione"]) after my_stack.append("Voldemort") --> my_stack = deque (["Draco", "Harry", "Hermione", "Voldemort"])