comp-think / 2021-2022

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

Lecture "Organising information: ordered structures", exercise 2 #15

Open essepuntato opened 2 years ago

essepuntato commented 2 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").

giorgimariachiara commented 2 years ago

The status of my_stack at the and of all the operations is: Draco, Harry, Hermione, Voldemort

elizastuglik commented 2 years ago

The status of the my_stack Will be: Draco, Harry, Hermione, Voldemort.

sotarega commented 2 years ago
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
print(my_stack)
['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']
tommasobattisti commented 2 years ago

my_stack will contain the following items: "Draco", "Harry", "Hermione", "Voldemort".

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

ManueleVeggi commented 2 years ago

The two commands my_stack.pop() will remove the last elements, which are 'Ron' and 'Severus. The command extend will add the 'Voldemort'at the end of the stack. The result is hence: ['Draco', 'Harry', 'Hermione', 'Voldemort']

OrsolaMBorrini commented 2 years ago
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop()
#The status of my_stack is now ["Draco", "Harry", "Hermione", "Ron"]
my_stack.pop()
#The status of my_stack is now ["Draco", "Harry", "Hermione"]
my_stack.append("Voldemort")
#The status of my_stack is now ["Draco", "Harry", "Hermione", "Voldemort"]
CarmenSantaniello commented 2 years ago
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop()

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

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

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

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

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

ghasempouri1984 commented 2 years ago
from collections import deque

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

my_stack.pop()
print(my_stack)

my_stack.pop()
print(my_stack)

my_stack.append("Voldemort")
print(my_stack)
angstigone commented 2 years ago

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

my_stack.pop()

stack becomes (["Draco", "Harry", "Hermione", "Ron"]).

my_stack.pop()

stack becomes (["Draco", "Harry", "Hermione"]).

my_stack.append(Voldemort).

stack becomes (["Draco", "Harry", "Hermione", "Voldemort"]).

MaddaGh commented 2 years ago

stack

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

NoraPs commented 2 years ago

from collections import deque from typing 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'])

francescabudel commented 2 years ago
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'])
sterenz commented 2 years ago

from collections import deque

my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) mystack.pop() # remove the rightmost item_ print(my_stack) deque(['Draco', 'Harry', 'Hermione', 'Ron'])

mystack.pop() # remove the rightmost item_ print(my_stack) deque(['Draco', 'Harry', 'Hermione'])

mystack.append("Voldemort") # add a new entry to the right side_ print(my_stack) deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])

RebeccaJillianBeattie commented 2 years ago

Visualising it like a stack:

my_stack Severus Ron Hermione Harry Draco ["Draco", "Harry", "Hermione", "Ron", "Severus"]

my_stack.pop() Ron Hermione Harry Draco ["Draco", "Harry", "Hermione", "Ron"]

my_stack.pop() Hermione Harry Draco ["Draco", "Harry", "Hermione"]

my_stack.append("Voldemort") Voldemort Hermione Harry Draco ["Draco", "Harry", "Hermione", "Voldemort"]

olgagolgan commented 2 years ago

image image

SaraVell1 commented 2 years ago

image

AnastasiyaSopyryaeva commented 2 years ago

INPUT from typing import Deque my_stack = Deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) print(my_stack)

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', 'Severus']) deque(['Draco', 'Harry', 'Hermione', 'Ron']) deque(['Draco', 'Harry', 'Hermione']) deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])

martasoricetti commented 2 years ago

The method .pop() removes the item on the top of the stack, that is the last item added in the stack ->

my_stack.pop() print(my_stack) deque(["Draco", "Harry", "Hermione", "Ron"])

my_stack.pop() print(my_stack) deque(["Draco", "Harry", "Hermione"])

The method .append() allows one to add an item on the top of the stack ->

my_stack.append("Voldemort") print(my_stack) deque(["Draco", "Harry", "Hermione", "Voldemort"])

chloeppd commented 2 years ago
Screenshot 2021-10-28 at 4 39 33 PM
AmeliaLamargese commented 2 years ago
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) 

with the execution of my_stack.pop() function, the last item will be removed,
in this case "Severus" will be deleted and returned
my_stack = ["Draco", "Harry", "Hermione", "Ron"]

after my_stack.pop() function, the last item will be removed,
in this case "Ron" will be deleted and returned
my_stack = ["Draco", "Harry", "Hermione"]

with my_stack.append("Voldemort") , "Voldemort" will be added to the stack as the last item
my_stack = ["Draco", "Harry", "Hermione", "Voldemort"]
ManuSrivastava1 commented 2 years ago

At the top of our stack is Severus and Draco is at the bottom. Following the FILO principle. The first pop command will remove from the top - Severus. The second pop command will remove from the top - rounds The append command will add to the top - Voldermort

Giving us the final stack as - deque['Draco','Harry','Hermoine','Voldermort']

sarabecchi commented 2 years ago

The status of my_stack after the execution of those operations is:

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

esercizio 3

Postitisnt commented 2 years ago

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

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

deque(['Draco', 'Harry', 'Hermione', 'Ron'])
my_stack.pop()
print(my_stack)

deque(['Draco', 'Harry', 'Hermione'])
my_stack.append("Voldemort")
print(my_stack)

deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
teragramgius commented 2 years ago
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) 
#a stack is characterized by Last In First Out strategy for adding and removing items. So the process is at it follows.

#I'll go two times with the pop method, to remove the last two items that are "Severus", "Ron"
my_stack.pop()
my_stack.pop()
#then I'll append to the stack the item (a string) "Voldemort"
my_stack.append("Voldemort")

#if I print my_stack, the stack will contain in order "Draco", "Harry", "Hermione", "Voldemort"

print (my_stack)

my_stack = ["Draco", "Harry", "Hermione", "Voldemort"]