使用渐近线完成程序

发布于 2024-12-05 13:00:46 字数 328 浏览 0 评论 0原文

这个问题可以用与语言无关的方式来回答,但我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永言不败 2024-12-12 13:00:46

这是一个粗略的尝试 - 根据 dig_more_objects 的性质,您可能需要调整条件:

import time
results = []
while True:
    mark = time.time()
    newobjs = dig_more_objects(obj)
    elapsed = time.time() - mark
    results.append((newobjs, elapsed))
    count = 0
    threshhold = 0
    for objs, elapsed in results[::-1]:
        count += len(objs)  # or +1 of dig_more_objects only returns one at a time
        threshhold += elapsed
        if threshhold > 60.0 and count < 10:
            break

Here's a rough stab at it -- depending on the nature of dig_more_objects you may need to adjust the condition:

import time
results = []
while True:
    mark = time.time()
    newobjs = dig_more_objects(obj)
    elapsed = time.time() - mark
    results.append((newobjs, elapsed))
    count = 0
    threshhold = 0
    for objs, elapsed in results[::-1]:
        count += len(objs)  # or +1 of dig_more_objects only returns one at a time
        threshhold += elapsed
        if threshhold > 60.0 and count < 10:
            break
2024-12-12 13:00:46

使用 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.

你的呼吸 2024-12-12 13:00:46

您可以尝试带有计时器的 multiton 模式设计这样一个类。请参阅了解想法

You can try the multiton pattern with a timer and design such a class. See this for an idea

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文