0
So guys, I’m doubtful about two queue algorithms, and I’d like a little help. The first doubt,
From collections import Deque
class Fila:
def __init__(self):
self.fila = deque() # atributo fila é um objeto do tipo deque
def insere(self, novo):
self.fila.append(novo) # uso append para inserir no final
def remove(self):
return self.fila.popleft() # popleft remove da esquerda (início)
def tamanho(self):
return len(self.fila) # usa len para calcular tamanho da fila
def vazia(self):
if len(self.fila) == 0:
return True
return False
def preferencial(self,novo):
self.fila.appendleft(novo)
and then we have a function that takes a queue as parameter and returns a list with all the items removed
def remove_todos(fila):
lista = []
fila = Fila()
while fila.vazia() == False:
fila.remove()
lista.append(fila)
return lista
and it returns an empty list instead of the queue items.
The second issue is the queue of a bank where only one teller works, and every two preferred customers, a non-preferential customer will be served.
def fila_banco(pessoas):
fila = Fila()
lista = []
for x in pessoas:
if x[1] >= 60:
fila.preferencial(x[0])
lista.append(x[0])
else:
fila.insere(x[0])
lista.append(x[0])
return lista
fila_banco([ ('Jorge', 22), ('Valter', 90), ('Alice', 88), ('Boris', 77) ])
who returns me ['Jorge', 'Valter', 'Alice', 'Boris']
what should be returned ('Valter', 'Alice', 'Jorge', 'Boris')
I appreciate the help.
Opa Robson, so I did the tests here and the remove_todos function continues to return an empty list, already the fila_banco it puts 'Jorge' at the end, and 'Boris' in front of 'Valter' and 'Alice', the right return would be 'Valter', 'Alice', 'Jorge', 'Boris'
– Thiago Andrade HoocKPeeR
Thiago talks all right. So I took your code, I made an algorithm for this problem, I did some tests and it seemed to me to be fine.I’ll edit the question adding the code so you can use and see if it works for you.
– Robson Silva
One thing I saw in your remove all code, apparently it should be in the Queue class. Because it is a queue method. Should be a method in this style ```python def remover_todos(self): list = [] while not self.empty(): list.append(self.remove()) Return list
– Robson Silva