使用渐近线完成程序
这个问题可以用与语言无关的方式来回答,但我使用的是 python (仅供参考)。
我正在运行一个无限循环,当在最后一分钟发现<10个新对象时需要终止。
例如:
while True:
newobjs = dig_more_objects(obj)
if less than 10 newobjs have been discovered over the last minute
break
编辑:问题是:我如何实现这一行:如果最后一分钟发现的新对象少于 10 个
This question could be answered in a language-agnostic fashion, but I am using python (fyi).
I am running an infinite loop which needs to terminate when during the last minute <10 new objects are discovered.
Eg:
while True:
newobjs = dig_more_objects(obj)
if less than 10 newobjs have been discovered over the last minute
break
EDIT: The question is this: How do I implement this line:if less than 10 newobjs have been discovered over the last minute
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个粗略的尝试 - 根据
dig_more_objects
的性质,您可能需要调整条件:Here's a rough stab at it -- depending on the nature of
dig_more_objects
you may need to adjust the condition:使用
collections.deque
来保存已发现对象的时间,如果弹出一个不到一分钟的值,并且双端队列中的项目数为 9 或更少,则跳出循环。如果超过 9 个项目,请不要忘记将其推回原位。超过一分钟的项目将被丢弃。Use a
collections.deque
to hold the times of the discovered objects, and if you pop a value that is less than one minute old and there are 9 or fewer items in the deque, break out of the loop. Don't forget to push it back in if there are more than 9 items though. Items older than a minute get discarded.您可以尝试带有计时器的 multiton 模式设计这样一个类。请参阅此了解想法
You can try the multiton pattern with a timer and design such a class. See this for an idea