在递归中只应用一次条件

Apply a condition only once in a recursion

我只想在复活中应用一次条件。我的数据结构如下

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

我想从中随机选择任何阶段并将状态更改为True。但是当真实阶段的总数为 3 时,我想随机将一个真实阶段更改为假。但只有一次。然后它应该继续将阶段变为 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)

您可以添加一个全局变量作为要获取的标志,如果您已经将其设置回 False,或者添加一个可选参数,例如 returnedToFalse=False 当你最终将一个设置为 false 时,将其设置为 true。

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

考虑对 process_internal() 进行内部调用以获取递归的当前状态,无论是第 0 阶段(False -> True)、第 1 阶段(True -> False)还是第 2 阶段(False) -> 正确)。

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)

代码是@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)