Doubt in lines

Asked

Viewed 120 times

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.

1 answer

0


Thiago all good?

So on your first question, your mehodo remove() from the Queue class, it returns the first element of the queue, and when you are using it, just call the method and don’t let your return, try to do it this way

def remove_todos(fila):
    lista = []
    fila = Fila()
    while not fila.vazia():
        lista.append(fila.remove())
    return lista

And since I can’t test code right now, I won’t be able to see your second question right now, but I took a look here, and try to get back in line..

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

fila_banco([ ('Jorge', 22), ('Valter', 90), ('Alice', 88), ('Boris', 77) ])

Because in the checks you add the larger than 60 at the beginning of the queue, and also you are adding the same in the list, however, at the end, and when you return this list it comes in the wrong order of call. Run these changes and see if it works when you can get a better look. However test and comment on what happened.

Edited code.

def fila_banco(pessoas):
    lista = [x[0] for x in pessoas if x[1] >=60]
    nao_pref = [x[0] for x in pessoas if not x[1] >=60][::-1]
    start = 2

    while nao_pref:
        lenght_lista = len(lista)
        if lenght_lista >= start:
            lista.insert(start, nao_pref.pop())
            start += 3

        else:
            lista.insert(lenght_lista +1, nao_pref.pop())


    return fila

In case I created a list in the order you want, you only need to turn into deck to call popleft. If it works comments here.

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

  • 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.