首页 > 解决方案 > waitinglist Class in Python

问题描述

I have a waiting list class. I am trying to work out how to implement the stillWaiting function which would return the total number of people waiting to go to a certain destination. I have included all the code so far.

class WaitingList:
    """
    An implementation of waiting lists, using Python lists.
    
    Each item in the list is an (integer, string) tuple 
    representing a group of people.
    """
 
    def __init__(self):
        """
        Initialise the waiting list to be empty.
        """
        self.items = []
    
    def hasGroup(self,  max, destination):
        """
        Return True if the waiting list has at least one group
        of no more than max people wanting to go to destination,
        otherwise return False.
        """
        for index in range(0,self.size()):
            thisGroup = self.items[index]
            if thisGroup[0] <= max and thisGroup[1] == destination:
               return True
        return False
    
    def put(self, groupSize, destination):
        """
        Add group to the end of the waiting list.
        """
        self.items.append((groupSize, destination))
    
    def size(self):
        """
        Return the number of groups in the waiting list.
        """
        return len(self.items)
    
    def take(self, max, destination):
        """
        Remove the first group of no more than max people
        wanting to go to destination.
        If there is no such group do nothing.
        """
        index = 0
        notFound = True
        while index < self.size() and notFound:
            thisGroup = self.items[index]
            if thisGroup[0] <= max and thisGroup[1] == destination:
                self.items.pop(index)
                notFound = False
            else:
                index = index + 1
                
    def stillWaiting(self, destination):
        """
        Return the total number of people on the waiting list 
        wanting to go to destination.
        """ 
        pass

TIA

标签: python

解决方案


You should look through all the groups (self.items) and check if any of those are looking to go to the specified destination, then return the sum of the sizes of these groups. If no such group can be found, return 0.


推荐阅读