Open essepuntato opened 2 years ago
The status of my_stack at the and of all the operations is: Draco, Harry, Hermione, Voldemort
The status of the my_stack Will be: Draco, Harry, Hermione, Voldemort.
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']
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'])
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']
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"]
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'])
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)
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"]).
deque(["Draco", "Harry", "Hermione", "Voldemort"])
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'])
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'])
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'])
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"]
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'])
The method
my_stack.pop() print(my_stack) deque(["Draco", "Harry", "Hermione", "Ron"])
my_stack.pop() print(my_stack) deque(["Draco", "Harry", "Hermione"])
The method
my_stack.append("Voldemort") print(my_stack) deque(["Draco", "Harry", "Hermione", "Voldemort"])
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"]
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']
The status of my_stack
after the execution of those operations is:
deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
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'])
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"]
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 ofmy_stack
after the execution of each of the following operations:my_stack.pop()
,my_stack.pop()
,my_stack.append("Voldemort")
.