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 3 #16

Open essepuntato opened 2 years ago

essepuntato commented 2 years ago

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

giorgimariachiara commented 2 years ago

The status of my_queue after all the operations is: Harry, Hermione, Ron, Severus

elizastuglik commented 2 years ago

After the execution of the operations the status of my_queue Will be: Harry, Hermione, Ron, Severus

sotarega commented 2 years ago
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']
tommasobattisti commented 2 years ago

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

ManueleVeggi commented 2 years ago

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

ghasempouri1984 commented 2 years ago

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)
CarmenSantaniello commented 2 years 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)

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".

angstigone commented 2 years ago

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

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

deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])

MaddaGh commented 2 years ago

queue

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

OrsolaMBorrini commented 2 years ago
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"]
sterenz commented 2 years ago

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

RebeccaJillianBeattie commented 2 years ago

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

olgagolgan commented 2 years ago

image image

SaraVell1 commented 2 years ago

image

AnastasiyaSopyryaeva commented 2 years ago

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

martasoricetti commented 2 years ago

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

PirjoElbrecht commented 2 years ago

from collections import deque # import statement my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])

my_queue.popleft() print(my_queue)

result deque(['Harry', 'Hermione', 'Ron', 'Severus'])

my_queue.append("Voldemort") print(my_queue)

result deque(['Harry', 'Hermione', 'Ron', 'Severus', 'Voldemort'])

my_queue.popleft() print(my_queue)

result deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])

chloeppd commented 2 years ago
Screenshot 2021-10-28 at 4 44 52 PM
AmeliaLamargese commented 2 years ago
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"]
ManuSrivastava1 commented 2 years ago

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

sarabecchi commented 2 years ago

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

deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])

esercizio 4

Postitisnt commented 2 years ago

deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])

sanyuezoe commented 2 years ago
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'])
teragramgius commented 2 years ago

_The status of my_queue after the execution of each operation will be myqueue = (["Hermione", "Ron", "Severus", "Voldemort"])