Open essepuntato opened 2 years ago
The status of my_queue after all the operations is: Harry, Hermione, Ron, Severus
After the execution of the operations the status of my_queue Will be: Harry, Hermione, Ron, Severus
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
print(my_queue)
['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']
my_queue.popleft() #removes first element from left (in this case 'Draco' and releases it)
print(my_queue)
['Harry', 'Hermione', 'Ron', 'Severus']
my_queue.append("Voldemort")
print(my_queue)
['Harry', 'Hermione', 'Ron', 'Severus', 'Voldemort']
my_queue.popleft()
print(my_queue)
['Hermione', 'Ron', 'Severus', 'Voldemort']
my_queue
will contain the following items:
"Hermione", "Ron", "Severus", "Voldemort".
from collections import deque
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_queue.popleft()
my_queue.append("Voldemort")
my_queue.popleft()
print(my_queue)
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
Even though not performed in a row, the two commands my_queue.popleft()
will remove the first elements, which are 'Draco'
and 'Harry'
. The command my_queue.append
will add the 'Voldemort'
at the end of the stack. The result is hence:
['Hermione', 'Ron', 'Severus', 'Voldemort']
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
from collections import deque
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
print(my_queue)
my_queue.popleft()
print(my_queue)
my_queue.append("Voldemort")
print(my_queue)
my_queue.popleft()
print(my_queue)
from collections import deque
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_queue.popleft()
my_queue.append("Voldemort")
my_queue.popleft()
print(my_queue)
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
Popleft() method removes "Draco" as it is the first item on the left of the queue, then append() method adds "Voldemort" as a new item and puts it at the rightmost side of the queue, lastly popleft() method once again removes the new first item "Harry".
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_queue.popleft()
queue becomes (["Harry", "Hermione", "Ron", "Severus"])
my_queue.append("Voldemort")
queue becomes (["Harry", "Hermione", "Ron", "Severus", "Voldemort"])
my_queue.popleft()
queue becomes (["Hermione", "Ron", "Severus", "Voldemort"])
my_queue = deque (["Draco", "Harry","Hermione", "Ron", "Severus"])
my_queue.popleft()
my_queue.append("Voldemort")
my_queue.popleft()
print(my_queue)
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
from collections import deque
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) my_queue.popleft() my_queue.append("Voldemort") my_queue.popleft() print (my_queue)
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
deque(["Hermione", "Ron", "Severus", "Voldemort"])
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_queue.popleft()
#The status of my_queue is now ["Harry", "Hermione", "Ron", "Severus"]
my_queue.append("Voldemort")
#The status of my_queue is now ["Harry", "Hermione", "Ron", "Severus", "Voldemort"]
my_queue.popleft()
#The status of my_queue is now ["Hermione", "Ron", "Severus", "Voldemort"]
from collections import deque
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) myqueue.popleft() # the first to arrive now leaves_ print(my_queue) # deque(['Harry', 'Hermione', 'Ron', 'Severus'])
myqueue.append("Voldemort") # Voldemort arrives_ print(my_queue) # deque(['Harry', 'Hermione', 'Ron', 'Severus', 'Voldemort'])
myqueue.popleft() # the second to arrive now leaves_ print(my_queue) # deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
my_queue ["Draco", "Harry", "Hermione", "Ron", "Severus"]
my_queue.popleft() ["Harry", "Hermione", "Ron", "Severus"]
my_queue.append("Voldemort") ["Harry", "Hermione", "Ron", "Severus", "Voldemort"]
my_queue.popleft() ["Hermione", "Ron", "Severus", "Voldemort"]
INPUT from typing import Deque
my_queue = Deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) print(my_queue)
my_queue.popleft() print(my_queue)
my_queue.append("Voldemort") print(my_queue)
my_queue.popleft() print(my_queue)
OUTPUT deque(['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']) deque(['Harry', 'Hermione', 'Ron', 'Severus']) deque(['Harry', 'Hermione', 'Ron', 'Severus', 'Voldemort']) deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
my_queue = deque (["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_queue.popleft ( ) # it removes the first item added print(my_queue) deque (["Harry", "Hermione", "Ron", "Severus"])
my_queue.append ( "Voldemort") print(my_queue) deque (["Harry", "Hermione", "Ron", "Severus", "Voldemort"])
my_queue.popleft ( ) print (my_queue) deque (["Hermione", "Ron", "Severus", "Voldemort"])
from collections import deque # import statement my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_queue.popleft() print(my_queue)
my_queue.append("Voldemort") print(my_queue)
my_queue.popleft() print(my_queue)
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
with the execution of my_queue.popleft() function, the first item will be removed,
in this case "Draco" will be deleted and returned
my_queue = ["Harry", "Hermione", "Ron", "Severus"]
after my_queue.append("Voldemort") function, "Voldemort" will be added to the queue as the last item
my_queue = ["Harry", "Hermione", "Ron", "Severus", "Voldemort"]
with the execution of my_queue.popleft() function, the first item will be removed,
in this case "Harry" will be deleted and returned
my_queue = ["Hermione", "Ron", "Severus", "Voldemort"]
pop.left will remove "Drace" from the stack(which is being treated like a queue at this point) Appeand will add "Voldermort" at top/right to the stack pop.left will remove "Harry" from the stack. leaving us the following :
deque['Hermoine','Ron','Severus','Voldermort']
The status of my_queue
after the execution of those operations is:
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
from collections import deque
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_queue.popleft()
print(my_queue)
deque(['Harry', 'Hermione', 'Ron', 'Severus'])
my_queue.append("Voldemort")
print(my_queue)
deque(['Harry', 'Hermione', 'Ron', 'Severus', 'Voldemort'])
my_queue.popleft()
print(my_queue)
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
_The status of my_queue after the execution of each operation will be myqueue = (["Hermione", "Ron", "Severus", "Voldemort"])
Consider to have a queue obtained by processing, one by one, the elements included in the list of the first exercise, i.e.
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
. Describe the status ofmy_queue
after the execution of each of the following operations:my_queue.popleft()
,my_queue.append("Voldemort")
,my_queue.popleft()
.