Queue - Python (order)

Asked

Viewed 432 times

0

Let’s simulate a queue situation at the bus terminal. Write a function that receives a NAME LIST (strings) that indicates the order of arrival of people to catch a bus from the terminal.

Your role should create a queue and insert people into it in order of arrival (the order in which they appear in the received list as a parameter).

Caution: there is a passenger, called Samuel, who caused problems in the terminal yesterday. Today, the inspector will stop you from boarding, so that he pays clarifications.

IE: you will add the names in the queue, but you should ignore the Samuel, who is not allowed to embark.

Then, removing one by one from the queue, create a new list that shows the order where people will get on the bus.

Your function will, at the end, return the boarding list.

Examples are: embarca(['Jose', 'Henrique', 'Carla']) --> returns the list [ 'Jose', 'Henrique', 'Carla' ], indicating the order of embarkation.

embarks(['Jose', 'Samuel', 'Carla']) --> returns the list ['Jose', 'Carla']

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
        else:
            return False

fila = Fila()
def embarca(pessoas):
    lista = []
    for i in range(len(pessoas)):
        if i != "Samuel":
            fila.insere(i)
        c = fila.remove()
        lista.append(c)
    return lista

1 answer

1

How Python already has the structure collections.deque that allows you to manage a queue, you don’t need to create a class for it. Just use it directly in your function:

from typing import List, Text
from collections import deque

def embarca(nomes: List[Text]) -> List[Text]:
    fila = deque()
    for nome in nomes:
        if nome != 'Samuel':
            fila.append(nome)
    return list(fila)

See working on Repl.it

In the function is initially created an empty queue, the names of the list of people that was received as parameter and when it is not Samuel is added the name in the queue; finally a list with the content of the queue is returned.

Note: The solution removes from the queue all people named Samuel, which in the real world would be very inconvenient with the Samuels, given that everyone would pay for what only one did. As it is not explicit in the statement a way to differentiate them, here is my apologies to all the others Samuels.

The result can be checked with:

assert embarca(['Jose', 'Henrique', 'Carla']) == [ 'Jose', 'Henrique', 'Carla' ]
assert embarca(['Jose', 'Samuel', 'Carla']) == ['Jose', 'Carla']

I believe that because it is an exercise that explicitly calls for the use of queues this is the expected solution, but the same problem could be solved in other ways in a simpler way. Using list comprehension, for example, it is possible to solve with a trivial line of code:

def embarca(nomes: List[Text]) -> List[Text]:
    return [nome for nome in nomes if nome != 'Samuel']

Browser other questions tagged

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