gpt4 book ai didi

function - 查找表中元素的每个组合(Lua/伪代码)

转载 作者:行者123 更新时间:2023-12-03 06:39:57 24 4
gpt4 key购买 nike

我正在尝试使用表中元素的每个组合执行一个函数。 (在 Lua 中)。表格和元素可以更改,但结构将保持不变。该表的组织方式使得其中的 [1] 将成为函数的第一个参数,依此类推。

如果这是我拥有的表格,

Table = {
[1] = {Player1, Player2}
[2] = {PlayerA, PlayerB, PlayerC}
[3] = {PlayerOne, PlayerTwo}
}

如果我手动写出来,它可能看起来像这样:(假设该函数名为 Exe)。

Exe(Player1, PlayerA, PlayerOne)
Exe(Player2, PlayerA, PlayerOne)
Exe(Player3, PlayerA, PlayerOne)

Exe(Player1, PlayerB, PlayerOne)
Exe(Player2, PlayerB, PlayerOne)
Exe(Player3, PlayerB, PlayerOne)

Exe(Player1, PlayerC, PlayerOne)
Exe(Player2, PlayerC, PlayerOne)
Exe(Player3, PlayerC, PlayerOne)


Exe(Player1, PlayerA, PlayerTwo)
Exe(Player2, PlayerA, PlayerTwo)
Exe(Player3, PlayerA, PlayerTwo)

Exe(Player1, PlayerB, PlayerTwo)
Exe(Player2, PlayerB, PlayerTwo)
Exe(Player3, PlayerB, PlayerTwo)

Exe(Player1, PlayerC, PlayerTwo)
Exe(Player2, PlayerC, PlayerTwo)
Exe(Player3, PlayerC, PlayerTwo)

但是,我不想把它写出来,它打破了我的一般经验法则,即如果您在程序中进行复制和粘贴,那么您就做错了。

因此,我想浏览该表并执行每一个可能的组合。这个问题使得表中可以(可能)有任意数量的表,并且表中的表也可能有无限数量的值。

例如,表格最终可能如下所示:

Table = {
[1] = {Player1, Player2}
[2] = {PlayerA}
[3] = {PlayerOne}
}

手动执行最终会像这样:

Exe(Player1, PlayerA, PlayerOne)
Exe(Player2, PlayerA, PlayerOne)

此外,表格最终可能会像这样:

Table = {
[1] = {Player1, Player2}
[2] = {PlayerA}
[3] = {PlayerOne}
[4] = {PlayerUno, PlayerDos}
[5] = {PlayerApple, PlayerBoy, PlayerCat, PlayerDog}
}

执行结果会是这样的..

Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerApple)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerApple)

Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerApple)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerApple)


Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerBoy)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerBoy)

Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerBoy)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerBoy)


Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerCat)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerCat)

Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerCat)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerCat)


Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerDog)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerDog)

Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerDog)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerDog)

正如你所看到的,我发现了一种模式...我能够将上面的“执行”事物划分为段/组,例如第 1 行和第 2 行有一个更改。然后,它们被复制到第 4 行和第 5 行,但下一个变量发生了更改。

如您所见,我无法将该模式放入代码中。我认为需要一些函数递归,但我不确定如何实现它或递归它。我想我必须使用以 ... 作为参数的函数和 unpack 函数,但我不确定这是如何工作的。

此外,之所以需要这样做,而不仅仅是手动复制和粘贴它(这实际上会更容易),是因为将生成表格的内容。

你们能帮帮我吗?

最佳答案

使用递归。

想象一个函数 map_all (fcn, tab, idx, ...)映射fcn所有表元素的乘积 tab[1]tab[idx]前缀为...

基本情况是 idx小于 1。在这种情况下,只需应用 fcn(...)

否则,map_all(fcn, tab, idx-1, <el>, ...)对于所有人<el>tab[idx]

function map_all (fcn, tab, idx, ...)
if idx < 1 then
fcn(...)
else
local t = tab[idx]
for i = 1, #t do map_all(fcn, tab, idx-1, t[i], ...) end
end
end

所以,

> Table = {
>> [1] = {'Player1', 'Player2'},
>> [2] = {'PlayerA', 'PlayerB', 'PlayerC'},
>> [3] = {'PlayerOne', 'PlayerTwo'}
>> }
> map_all(print, Table, #Table)
Player1 PlayerA PlayerOne
Player2 PlayerA PlayerOne
Player1 PlayerB PlayerOne
Player2 PlayerB PlayerOne
Player1 PlayerC PlayerOne
Player2 PlayerC PlayerOne
Player1 PlayerA PlayerTwo
Player2 PlayerA PlayerTwo
Player1 PlayerB PlayerTwo
Player2 PlayerB PlayerTwo
Player1 PlayerC PlayerTwo
Player2 PlayerC PlayerTwo

> Table = {
>> [1] = {'Player1', 'Player2'},
>> [2] = {'PlayerA'},
>> [3] = {'PlayerOne'}
>> }
> map_all(print, Table, #Table)
Player1 PlayerA PlayerOne
Player2 PlayerA PlayerOne

> Table = {
>> [1] = {'Player1', 'Player2'},
>> [2] = {'PlayerA'},
>> [3] = {'PlayerOne'},
>> [4] = {'PlayerUno', 'PlayerDos'},
>> [5] = {'PlayerApple', 'PlayerBoy', 'PlayerCat', 'PlayerDog'},
>> }
> map_all(print, Table, #Table)
Player1 PlayerA PlayerOne PlayerUno PlayerApple
Player2 PlayerA PlayerOne PlayerUno PlayerApple
Player1 PlayerA PlayerOne PlayerDos PlayerApple
Player2 PlayerA PlayerOne PlayerDos PlayerApple
Player1 PlayerA PlayerOne PlayerUno PlayerBoy
Player2 PlayerA PlayerOne PlayerUno PlayerBoy
Player1 PlayerA PlayerOne PlayerDos PlayerBoy
Player2 PlayerA PlayerOne PlayerDos PlayerBoy
Player1 PlayerA PlayerOne PlayerUno PlayerCat
Player2 PlayerA PlayerOne PlayerUno PlayerCat
Player1 PlayerA PlayerOne PlayerDos PlayerCat
Player2 PlayerA PlayerOne PlayerDos PlayerCat
Player1 PlayerA PlayerOne PlayerUno PlayerDog
Player2 PlayerA PlayerOne PlayerUno PlayerDog
Player1 PlayerA PlayerOne PlayerDos PlayerDog
Player2 PlayerA PlayerOne PlayerDos PlayerDog
>

关于function - 查找表中元素的每个组合(Lua/伪代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13059491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com