- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 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/
这是代码片段。 请说出这种用小内存存储大数据的算法是什么。 public static void main(String[] args) { long longValue = 21474836
所以我使用 imap 从 gmail 和 outlook 接收电子邮件。 Gmail 像这样编码 =?UTF-8?B?UmU6IM69zq3OvyDOtc68zrHOuc67IG5ldyBlbWFpb
很久以前就学会了 C 代码;想用 Scheme 尝试一些新的和不同的东西。我正在尝试制作一个接受两个参数并返回两者中较大者的过程,例如 (define (larger x y) (if (> x
Azure 恢复服务保管库有两个备份配置选项 - LRS 与 GRS 这是一个有关 Azure 恢复服务保管库的问题。 当其驻留区域发生故障时,如何处理启用异地冗余的恢复服务保管库?如果未为恢复服务启
说,我有以下实体: @Entity public class A { @Id @GeneratedValue private Long id; @Embedded private
我有下一个问题。 我有下一个标准: criteria.add(Restrictions.in("entity.otherEntity", getOtherEntitiesList())); 如果我的
如果这是任何类型的重复,我会提前申请,但我找不到任何可以解决我的具体问题的内容。 这是我的程序: import java.util.Random; public class CarnivalGame{
我目前正在使用golang创建一个聚合管道,在其中使用“$ or”运算符查询文档。 结果是一堆需要分组的未分组文档,这样我就可以进入下一阶段,找到两个数据集之间的交集。 然后将其用于在单独的集合中进行
是否可以在正则表达式中创建 OR 条件。 我正在尝试查找包含此类模式的文件名列表的匹配项 第一个案例 xxxxx-hello.file 或者案例二 xxxx-hello-unasigned.file
该程序只是在用户输入行数时创建菱形的形状,因此它有 6 个 for 循环; 3 个循环创建第一个三角形,3 个循环创建另一个三角形,通过这 2 个三角形和 6 个循环,我们得到了一个菱形,这是整个程序
我有一个像这样的查询字符串 www.google.com?Department=Education & Finance&Department=Health 我有这些 li 标签,它们的查询字符串是这样
我有一个带有静态构造函数的类,我用它来读取 app.config 值。如何使用不同的配置值对类进行单元测试。我正在考虑在不同的应用程序域中运行每个测试,这样我就可以为每个测试执行静态构造函数 - 但我
我正在寻找一个可以容纳多个键的容器,如果我为其中一个键值输入保留值(例如 0),它会被视为“或”搜索。 map, int > myContainer; myContainer.insert(make_
我正在为 Web 应用程序创建数据库,并正在寻找一些建议来对可能具有多种类型的单个实体进行建模,每种类型具有不同的属性。 作为示例,假设我想为“数据源”对象创建一个关系模型。所有数据源都会有一些共享属
(1) =>CREATE TABLE T1(id BIGSERIAL PRIMARY KEY, name TEXT); CREATE TABLE (2) =>INSERT INTO T1 (name)
我不确定在使用别名时如何解决不明确的列引用。 假设有两个表,a 和 b,它们都有一个 name 列。如果我加入这两个表并为结果添加别名,我不知道如何为这两个表引用 name 列。我已经尝试了一些变体,
我的查询是: select * from table where id IN (1,5,4,3,2) 我想要的与这个顺序完全相同,不是从1...5,而是从1,5,4,3,2。我怎样才能做到这一点? 最
我正在使用 C# 代码执行动态生成的 MySQL 查询。抛出异常: CREATE TABLE dump ("@employee_OID" VARCHAR(50)); "{"You have an er
我有日期 2016-03-30T23:59:59.000000+0000。我可以知道它的格式是什么吗?因为如果我使用 yyyy-MM-dd'T'HH:mm:ss.SSS,它会抛出异常 最佳答案 Sim
我有一个示例模式,它的 SQL Fiddle 如下: http://sqlfiddle.com/#!2/6816b/2 这个 fiddle 只是根据 where 子句中的条件查询示例数据库,如下所示:
我是一名优秀的程序员,十分优秀!