comp-think / 2023-2024

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. 2023/2024).
14 stars 0 forks source link

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

Open essepuntato opened 8 months ago

essepuntato commented 8 months ago

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 of my_queue after the execution of each of the following operations: my_queue.popleft(), my_queue.append("Voldemort"), my_queue.popleft().

qwindici commented 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.

vattelalberto commented 8 months ago
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"]

saramadonia commented 8 months ago
frammenti commented 8 months ago
>>> 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'])
Liber-R commented 8 months ago

Screenshot 2023-10-23 144525

MariaFrancesca6 commented 8 months ago
#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'])
matildepassafaro commented 8 months ago

Current status of my_queue: ["Hermione", "Ron", "Severus", "Voldemort"]

alicepiazzi commented 8 months ago
Schermata 2023-10-24 alle 22 54 27
ThIheb commented 8 months ago

exercice3 The result is ["Hermione", "Ron", "Severus", "Voldemort"]

Alice-Ant commented 8 months ago
Screenshot 2023-10-28 at 00 08 53
rufferbaraldi commented 8 months ago

my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) my_queue.popleft()

Draco is removed

my_queue.append("Voldemort")

Voldemort is added after Severus

my_queue.popleft()

Harry is removed, now the order is "Hermione, Ron, Severus, Voldemort"

parkful commented 8 months ago

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)

#15
simocasaz commented 8 months ago
# 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.
lucreziapograri commented 8 months ago

my_queue = deque(["Hermione", "Ron", "Severus", "Voldemort"])

Chiaramartina commented 8 months ago

my_queue.popleft()

my_queue.append("Voldemort")

my_queue.popleft()

Schermata 2023-11-04 alle 12 18 28
Sergpoipoip commented 7 months ago

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

enricabruno commented 7 months ago
OrganisingInfo-ex3
annapasetto0 commented 7 months ago

hp_queue

VirginiaDa00 commented 7 months ago
es 3 lezione 5
CarlaMenegat commented 7 months ago

Captura de Tela 2023-11-19 às 16 14 13