python - 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
解决方案
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.
推荐阅读
- php - 如何将默认表格内容显示为不同的,当单击按钮时,UI 中的同一个表格中的表格内容应该不同?
- wordpress - WPBakery Grid builder 获取帖子内容
- mongodb - MongoDB $从带有过滤器的数组中提取
- javascript - 带有ajax的表单-没有刷新页面的消息表单php
- r - R中的HSD Tukey测试错误
- javascript - 尽管已绑定,但在 onClick 处未定义 React 函数?
- html - 如何在图像上创建响应式 div?
- batch-file - findstr 在多个值上
- java - JPQL:直接查询对象列表
- php - 从 C# 到 PHP 的正则表达式