在递归中仅应用一次条件

发布于 2025-01-24 04:15:29 字数 1286 浏览 0 评论 0原文

我只想在防护中应用一次条件。我的数据结构如下,

stages = {"stage_1": False, "stage_2":False, "stage_3":False,"state_4": False}

我想从中随机选择任何阶段,然后将状态更改为true。但是,当真实阶段的总数为3时,我想随机将真实阶段更改为false。但是只有一次。然后,它应该继续将阶段转为true。当所有4个阶段都是正确的时。递归过程停止。 我该怎么做? 我尝试了以下代码。但这还不完整。

def process(stages):
    all_stages = [stage for stage, status in stages.items() if status == False]
    if len(all_stages) !=0:
        print(all_stages)
        select_ = random.choice(all_stages)
        print("\tselected stage: ",select_)
        
        stages[select_] = True
        process(stages)
    else:
        print("Done")
        print(stages)

process(stages)

这在不添加额外条件的情况下起作用。我尝试了以下一个。但这不起作用

def process(stages):
    all_stages = [stage for stage, status in stages.items() if status == False]
    if len(all_stages) !=0:
        print(all_stages)
        select_ = random.choice(all_stages)
        print("\tselected stage: ",select_)
        
        stages[select_] = True

        if len(all_stages) == 1:
            select_ = random.choice([stage for stage, status in stages.items() if status == True])
            stages[select_] = False
        process(stages)
    else:
        print("Done")
        print(stages)

process(stages)

I want to apply a condition inside a resursion only once. My data structure looks as follows

stages = {"stage_1": False, "stage_2":False, "stage_3":False,"state_4": False}

I want to pick any stage randomly from it and change the status to True. But when total number of true stage is 3 I want to randomly change a True stage to False. But only once. Then it should continue to turn stages in to True. When all 4 stages are true. The recursion process stops.
How can I do that ?
I have tried the following code. but it is not complete.

def process(stages):
    all_stages = [stage for stage, status in stages.items() if status == False]
    if len(all_stages) !=0:
        print(all_stages)
        select_ = random.choice(all_stages)
        print("\tselected stage: ",select_)
        
        stages[select_] = True
        process(stages)
    else:
        print("Done")
        print(stages)

process(stages)

This works without adding that extra condition. I have tried the following one. But that does not work

def process(stages):
    all_stages = [stage for stage, status in stages.items() if status == False]
    if len(all_stages) !=0:
        print(all_stages)
        select_ = random.choice(all_stages)
        print("\tselected stage: ",select_)
        
        stages[select_] = True

        if len(all_stages) == 1:
            select_ = random.choice([stage for stage, status in stages.items() if status == True])
            stages[select_] = False
        process(stages)
    else:
        print("Done")
        print(stages)

process(stages)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

想挽留 2025-01-31 04:15:29

代码是@noblockhit的建议

stages = {"stage_1": False, "stage_2":False, "stage_3":False,"state_4": False}

status = True
def process(stages):
    global status
    print(status)
    all_stages = [stage for stage, status in stages.items() if status == False]
    if len(all_stages) !=0:
        print(all_stages)
        select_ = random.choice(all_stages)
        print("\tselected stage: ",select_)
        
        stages[select_] = True

        if status:
            if len(all_stages) == 1:
                select_ = random.choice([stage for stage, status in stages.items() if status == True])
                stages[select_] = False
                status = False
        process(stages)
    else:
        print("Done")
        print(stages)

process(stages)

the code is the suggestion from @NoBlockhit

stages = {"stage_1": False, "stage_2":False, "stage_3":False,"state_4": False}

status = True
def process(stages):
    global status
    print(status)
    all_stages = [stage for stage, status in stages.items() if status == False]
    if len(all_stages) !=0:
        print(all_stages)
        select_ = random.choice(all_stages)
        print("\tselected stage: ",select_)
        
        stages[select_] = True

        if status:
            if len(all_stages) == 1:
                select_ = random.choice([stage for stage, status in stages.items() if status == True])
                stages[select_] = False
                status = False
        process(stages)
    else:
        print("Done")
        print(stages)

process(stages)
梨涡少年 2025-01-31 04:15:29

与许多递归问题一样,在功能调用中添加状态将对您有很大帮助。

考虑对process_internal()进行内部呼吁,该将获得递归的当前状态,无论是阶段0(false -> true),阶段1(true - > false)还是阶段2 (false--> true)。

import random

def flip_random_state(stages, relevant_bool_val):
    [print(f"Stage: {key} with status {value}") for key, value in stages.items()]
    relevant_stages = [stage for stage, status in stages.items() if status == relevant_bool_val]
    selected_stage = random.choice(relevant_stages)
    print(f"\tSelected stage: {selected_stage}\n")
    stages[selected_stage] = not stages[selected_stage]
    return stages

def count_stages_with_status(stages, input_status):
    return len([stage for stage, status in stages.items() if status == input_status])

def process_internal(stages, state):
    print(f"State: {state}")
    if state == 2 and count_stages_with_status(stages, True) == 4:
        print("Done")
        print(stages)
        return
    
    if state == 0 or state == 2:
        relevant_bool_val = False
    else:
        relevant_bool_val = True

    if state == 1 and count_stages_with_status(stages, False) == 4:
        state = 2
        return process_internal(stages, state)
    
    if state == 0 and count_stages_with_status(stages, True) == 3:
        state = 1
        return process_internal(stages, state)

    stages = flip_random_state(stages, relevant_bool_val)

    return process_internal(stages, state)

def process(stages):
    return process_internal(stages, 0)

stages = {"stage_1": False, "stage_2": False, "stage_3": False,"state_4": False}
process(stages)

As with many recursion problems, adding a state to your function call will help you a lot.

Consider making an inner call to process_internal() that would get the current state of the recursion, whether it's stage 0 (False -> True), stage 1 (True -> False) or stage 2 (False -> True).

import random

def flip_random_state(stages, relevant_bool_val):
    [print(f"Stage: {key} with status {value}") for key, value in stages.items()]
    relevant_stages = [stage for stage, status in stages.items() if status == relevant_bool_val]
    selected_stage = random.choice(relevant_stages)
    print(f"\tSelected stage: {selected_stage}\n")
    stages[selected_stage] = not stages[selected_stage]
    return stages

def count_stages_with_status(stages, input_status):
    return len([stage for stage, status in stages.items() if status == input_status])

def process_internal(stages, state):
    print(f"State: {state}")
    if state == 2 and count_stages_with_status(stages, True) == 4:
        print("Done")
        print(stages)
        return
    
    if state == 0 or state == 2:
        relevant_bool_val = False
    else:
        relevant_bool_val = True

    if state == 1 and count_stages_with_status(stages, False) == 4:
        state = 2
        return process_internal(stages, state)
    
    if state == 0 and count_stages_with_status(stages, True) == 3:
        state = 1
        return process_internal(stages, state)

    stages = flip_random_state(stages, relevant_bool_val)

    return process_internal(stages, state)

def process(stages):
    return process_internal(stages, 0)

stages = {"stage_1": False, "stage_2": False, "stage_3": False,"state_4": False}
process(stages)
折戟 2025-01-31 04:15:29

您可以将一个全局变量添加为标志,要获得的标志,您已经将一个变量设置为false,或者添加一个可选参数,例如rectionTofalse = false
当您最终将一个设置为False时,将其设置为True。

You could add a global variable as a flag to get, wheather you already set one back to False, or add an optional parameter like returnedToFalse=False
And set it to true when you eventually set one to false already.

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