Open essepuntato opened 8 months ago
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
First, the first element was removed with popleft(), then an element was appended at the end of the queue with append and another element was removed from the start of the queue.
my_queue.popleft()
->my_queue
will be ["Harry, "Hermione, "Ron", "Severus"]
my_queue.append("Voldemort")
->my_queue
will be ["Harry, "Hermione, "Ron", "Severus", "Voldemort"]
my_queue.popleft()
->my_queue
will be ["Hermione, "Ron", "Severus", "Voldemort"]
>>> from collections import deque
>>> my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
#1
>>> my_queue.popleft()
'Draco'
#First element added (first indexed) to the queue is removed
#through popleft() method.
>>> print(my_queue)
deque(['Harry', 'Hermione', 'Ron', 'Severus'])
#2
>>> my_queue.append("Voldemort")
#Through the append() method, a new element is added at the end
#(after last indexed item) of the queue.
>>> print(my_queue)
deque(['Harry', 'Hermione', 'Ron', 'Severus', 'Voldemort'])
#3
>>> my_queue.popleft()
'Harry'
#By applying popleft() again, the second element originally
#added to the queue, now first indexed, is removed.
>>> print(my_queue)
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
#import deque from collections
>>> from collections import deque
#the given queue
>>> my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
#remove "Draco" from the left (FIFO strategy)
>>> my_queue.popleft()
'Draco'
>>> print(my_queue)
deque(['Harry', 'Hermione', 'Ron', 'Severus'])
#add "Voldemort" at the right of the queue
>>> my_queue.append("Voldemort")
>>> print(my_queue)
deque(['Harry', 'Hermione', 'Ron', 'Severus', 'Voldemort'])
#remove "Harry" from the left (FIFO strategy)
>>> my_queue.popleft()
'Harry'
>>> print(my_queue)
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])
Current status of my_queue: ["Hermione", "Ron", "Severus", "Voldemort"]
The result is ["Hermione", "Ron", "Severus", "Voldemort"]
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) my_queue.popleft()
my_queue.append("Voldemort")
my_queue.popleft()
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)
# The first exercise
potter_characters = list()
potter_characters.append("Harry")
potter_characters.append("Draco")
potter_characters.append("Hermione")
potter_characters.append("Ron")
potter_characters.append("Severus")
potter_characters.sort()
# The third exercise
my_queue = deque(potter_characters) # Creating the queue by processing the elements included in the list of the first exercise
print(my_queue) # Printing to check if everything is ok
my_queue.popleft() # The first element on the left ("Draco") is removed.
print(my_queue) # Printing to see the corrent status. Now "Draco" is no longer in the queue.
my_queue.append("Voldemort") # "Voldemort" is added at the end (that is, the right) of the queue.
print(my_queue) # Now "Voldemort" is behind "Severus".
my_queue.popleft() # The first element on the left ("Harry") is removed.
print(my_queue) # Now there are four elements in the queue.
my_queue = deque(["Hermione", "Ron", "Severus", "Voldemort"])
my_queue.popleft()
my_queue.append("Voldemort")
my_queue.popleft()
As queue sotres elements in First-In-First-Out (FIFO) manner, the execution of the following operations my_queue.popleft(), my_queue.append("Voldemort"), my_queue.popleft()
will result in this queue: my_queue = deque(["Hermione", "Ron", "Severus", "Voldemort"])
Consider having 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()
.