从等于一个数字的一个数字的有限列表中生成所有组合

2024-09-30 12:16:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图根据您提供给代码的数据生成一个“非玩家游戏”。你知道吗

在游戏Zero Escape 999中,有9个玩家,每个玩家携带一个从1到9的索引。由于数字门阻塞了玩家的路径,玩家被迫分成若干组,这需要3到5个玩家对他们的索引求和并创建一个数字根。示例:

四号门和五号门。你知道吗

可用玩家:123456789。你知道吗

许多可行的分拆之一:

第四组:1237=1+2+3+7=13=1+3=4,玩家1、2、3、7可以通过门

第五组:45689=4+5+6+8+9=32=3+2=5,玩家4、5、6、8、9可以通过门。你知道吗

这就是我的代码的作用: 目标是从当前可用的玩家列表中生成一长串可能的组合,并显示所有结果,其中:

A)有玩家掉队,以及

B)没有球员掉队。你知道吗

当前代码段按预期工作,但是,它不能说明超过2个门,并且如果仅提供一个门编号,则不允许显示所有可用的组合。我被困在这一部分,因为我不能想出一个好的,递归的方法,将解释3门分裂的可能性在一个体面的方式。你知道吗

最小组数应为2,最大组数应等于可用门的数量。你知道吗

因此,如果有三个门,脚本将从两到三个团队中吐出。如果有四个门,它将显示两到四个小组,等等

为了尽量减少显示的数据量,我使用了一个较小的玩家池123456;每个门最少2个玩家,最多4个,2个编号的门4和8。这是控制台输出:

Please input the total players in a single string, e.g. 123456789. 123456 Please input lowest amount of players per door. 2 Please input highest amount of players per door. 4 Please input amount of doors. 2 Please input number for door 1. 4 Please input number for door 2. 8 The digital root of the doors is 3. 4: 13, 8: 26 left behind: 45 4: 13, 8: 2456 left behind: 4: 256, 8: 134 left behind: 4: 346, 8: 125 left behind: 4: 1246, 8: 35 left behind: 4: 1345, 8: 26 left behind: Displaying all possible combos.

from itertools import combinations

def digit_root(n): 
    return (n - 1) % 9 + 1 if n else 0


players = input('Please input the total players in a single string, e.g. 123456789. ')
player_combinations = [''.join(l) for i in range(len(players)) for l in combinations(players, i+1)]
minplayers = int(input("Please input lowest amount of players per door. "))
maxplayers = int(input("Please input highest amount of players per door. "))
while True:
    doortotal = int(input("Please input amount of doors. "))

    doors = []
    for i in range(doortotal):
        doors.append({"num": int(input("Please input number for door {}. ".format(i+1))), "combos": []})

        for combo in player_combinations:
            if len(combo) >= minplayers and len(combo) <= maxplayers and digit_root(int(combo)) == doors[i]["num"]:
                doors[i]["combos"].append(combo)

    door_root = digit_root(sum([door["num"] for door in doors]))
    print("The digital root of the doors is {}.".format(door_root))

    # for door in doors:
    #   print('Door #{} individual combos:'.format(door["num"]))
    #   print('\n'.join(door["combos"]))

    def get_combo(comboA, comboB):
        if any(a for a in comboA if a in comboB) == True: #We can't have the same player entering one or several doors at once
            return None
        combo = comboA + comboB
        dif = ''.join([a for a in players if not (a in combo)])
        print("{}: {}, {}: {} \tleft behind: {}".format(doors[0]["num"], comboA, doors[1]["num"], comboB, dif))
        return combo

    for comboA in doors[0]["combos"]:
        for comboB in doors[1]["combos"]:
            get_combo(comboA, comboB)

    input("Displaying all possible combos.")

该脚本预计将说明2个以上的门,并列出所有可用的组合为2到门计数团队。我需要帮助来创建一个可行的递归循环,它以这样的方式运行:

3 doors - 3,7,8.

显示可能的组合:

Doors 3+7, doors 3+8, doors 7+8, doors 3+7+8


Tags: ofinforinput玩家rootleftamount

热门问题