首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中,您如何检查脑f*ck程序是否处于超过5秒的循环中?

在python中,您如何检查脑f*ck程序是否处于超过5秒的循环中?
EN

Stack Overflow用户
提问于 2022-08-27 19:40:19
回答 1查看 62关注 0票数 0

我正在制作一个程序,在python中生成一个随机的Brainf*ck程序并运行它。我有一个解释器,我需要它来检查长循环(超过5秒)。

翻译是:

代码语言:javascript
复制
while i < len(code):
    if code[i] == '<':
        if pointerLocation > 0:
            pointerLocation -= 1
    elif code[i] == '>':
        pointerLocation += 1
        if len(array) <= pointerLocation:
            array.append(0)
    elif code[i] == '+':
        array[pointerLocation] += 1
    elif code[i] == '-':
        if array[pointerLocation] > 0:
            array[pointerLocation] -= 1
    elif code[i] == '.':
        print(array[pointerLocation], chr(array[pointerLocation]))
        result += chr(array[pointerLocation])
    elif code[i] == ',':
        #x = input("Input (1 CHARACTER!):")
        x = 'h'
        #increase time if user inputs
        t1 += 5
        try:
            y = int(x)
        except ValueError:
            y = ord(x)
        array[pointerLocation] = y
    elif code[i] == '[':
        if array[pointerLocation] == 0:
            open_braces = 1
            while open_braces > 0 and i+1 < len(code):
                i += 1
                if code[i] == '[':
                    open_braces += 1
                elif code[i] == ']':
                    open_braces -= 1
    elif code[i] == ']':
        # you don't need to check array[pointerLocation] because the matching '[' will skip behind this instruction if array[pointerLocation] is zero
        open_braces = 1
        while open_braces > 0:
            if t > 5:
                return [result,code,t]
                print(123)
            i -= 1
            if code[i] == '[':
                open_braces -= 1
            elif code[i] == ']':
                open_braces += 1
        # i still gets incremented in your main while loop
        i -= 1
    i += 1

很抱歉有这么长的片段,但我相信这一切都是必要的。

EN

回答 1

Stack Overflow用户

发布于 2022-08-27 19:43:39

当您进入循环5秒时设置一个告警。当你退出时取消它。如果信号触发,你就处理它。

一个较弱的版本是获得进入循环的时间,然后检查每个迭代,看看每次迭代是否超过5秒。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73513930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档