gpt4 book ai didi

javascript - 基于外部逻辑的具有索引增量限制的可变数量的嵌套 While 循环

转载 作者:行者123 更新时间:2023-12-05 04:25:27 25 4
gpt4 key购买 nike

在 Javascript 中,我试图遍历索引数组 indexArray。这个数组可以是任意长度,但为了便于解释,我将使用长度 3 indexArray.length = 3

我有一个外部函数:getResult(indexArray),它返回一个整数。

我想递归循环 indexArray 调用 getResult(indexArray) 并递增每个索引,直到满足条件:getResult(indexArray) > 5

初始数组始终是 1 的数组:indexArray = [1, 1, 1]。我希望索引按以下方式从最后一个元素递增到第 0 个元素:[1,1,1], [1,1,2], [1,1,3], [1,1,4] *满足条件* [1,2,1], [1,2 ,2], [1,2,3], ...依此类推,直到整数上的每个组合都满足条件。

我找到了 this stackoverflow question这帮助我制定了最初的尝试,但是由于我不知道索引的约束,for 循环不适用于我的用例。

到目前为止,这是我尝试过的:

numVars = 3;
currentIndices = Array(numVars).fill(1);
end = false

function callManyTimes(indices, func) {
doCallManyTimes(indices, func, [], 0);
}

function doCallManyTimes(indices, func, args, index) {
if (indices.length == 0) {
func(args);
end = true;
} else {
var rest = indices.slice(1);

args[index] = 1;
while(!end){
currentIndices[index] = args[index]
currentVal = func(currentIndices);

if(currentVal > 5){
doCallManyTimes(rest, func, args, index + 1)
}

++args[index];
}
}
}

function getResult(indices){
res = indices.reduce((partialSum, a) => partialSum + a, 0);
console.log(currentIndices);
return res;
}

callManyTimes(currentIndices, getResult);

我得到的输出(通过记录每个循环的索引)是:

[ 1, 1, 1 ]
[ 2, 1, 1 ]
[ 3, 1, 1 ]
[ 4, 1, 1 ]
[ 4, 1, 1 ]
[ 4, 1, 1 ]
[ 4, 1, 1 ]

很明显,我的循环条件有问题,但我似乎无法理解为什么递归没有遍历不同的索引。或者为什么它会重复其最终状态 4 次。如果有人可以帮助解决这个问题,将不胜感激。谢谢!

编辑:我的预期/期望输出是:

[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 1, 4] //condition met here
[1, 2, 1]
[1, 2, 2]
[1, 2, 3] //condition met here
[1, 3, 1]
[1, 3, 2] //condition met here
[1, 4, 1] //condition met here
[2, 1, 1]
[2, 1, 2]
[2, 1, 3] //condition met here
[2, 2, 1]
[2, 2, 2] //condition met here
[2, 3, 1] //condition met here
[3, 1, 1]
[3, 1, 2] //condition met here
[3, 2, 1] //condition met here
[4, 1, 1] //condition met here

编辑 #2:减少但准确的条件计算逻辑

我存储了 currentVal 的先前值 (previousVal),而不是行 currentVal > 5,并检查以下内容:(currentVal > 0) && ((currentVal/previousVal) > .99)。如果满足这两个条件,我想增加索引数组,如前所述。下面是我的 getResults(indices) 函数的一个粗略版本,为了使逻辑尽可能接近原始逻辑,删除了无关的步骤。

const BigNumber = require("bignumber.js");

function getResults(indices){
sum = BigNumber(0);

for(i = 0; i < indices.length; i++){
fiveTerm = indices.length - 1 - i;
fiveValue = BigNumber(5).pow(fiveTerm);

var threeValue;
if(i == 0){
threeValue = BigNumber(1);
} else {
threeIndices = indices.slice(-i);
threeIndexSum = threeIndices.reduce((partialSum, a) => partialSum + a, 0);
threeValue = BigNumber(3).pow(threeIndexSum);
}

currentTerm = fiveValue.times(threeValue);
sum = sum.plus(currentTerm);

}

indexSum = indices.reduce((partialSum, a) => partialSum + a, 0);
divisor = (BigNumber(3).pow(indexSum)).minus(BigNumber(5).pow(indices.length))

result = sum.div(divisor);

console.log("Indices: " + indices + "\nResult: " + result.toFixed())
return result;
}

我希望这个编辑有助于澄清我希望迭代器递增的条件。

最佳答案

首先我想使用递归。但我发现自己一直在做 for 循环并像人类计数一样思考,所以我决定改编另一个 answer来自同一个问题页面。

解释:

我们从最右边的数字开始。评估 getResult 函数然后增加计数直到我们达到 value > 5(或另一个停止条件)。然后我们重置我们位置上的数字,并将位置向左移动一步(比如从 099 数到 100)。我们一直这样做,直到我们通过最左边的位置。没有递归。

var numVars = 3;
var currentIndices = Array(numVars).fill(1);

function loop(arr, getResult, stopCondition) {
var pos;
do {
var value = getResult(arr);
print(arr)
pos = arr.length - 1;
arr[pos]++;
while (pos >= 0 && stopCondition(value)) {
arr[pos] = 1;
pos--;
if (pos >= 0) {
arr[pos]++;
value = getResult(arr);
if (stopCondition(value)) {
print(arr)
}
}
}
} while (pos >= 0);
}

function getResult(indices) {
return indices.reduce((partialSum, a) => partialSum + a, 0);
}

function print(indices) {
console.log("" + indices);
}

function stopCondition(value) {
return value > 5;
}

loop(currentIndices, getResult, stopCondition);
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}

关于javascript - 基于外部逻辑的具有索引增量限制的可变数量的嵌套 While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73262369/

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