function findSequence(goal) {
function find(start, history) {
if (start == goal)
return history;
else if (start > goal)
return null;
else {
var ret = find(start + 5, "(" + history + " + 5)");
if (ret == null)
ret = find(start * 3, "(" + history + " * 3)");
return ret;
}
}
return find(1, "1");
}
print(findSequence(24));
: where 而不是表达式,您只能得到 + 和 * 的字符串,
function findSequence(goal) {
function find(start, history) {
if (start == goal)
return history;
else if (start > goal)
return null;
else {
var ret = find(start + 5, history + "+");
if (ret == null)
ret = find(start * 3, history + "*");
return ret;
}
}
return find(1, "1");
}
print(findSequence(24));
并且请注意,作为示例,它非常复杂,因为它使用了闭包(本地定义的函数)。
You have an "output" string (((1 * 3) + 5) * 3), a goal 24 and a first character 1. The 1 is the 1 in 1 * 3. This program will assemble a string containing a math expression, adding (), *3 and +5 to try to obtain the goal. The two parameters of the find function are the current total and the expression that will generate the total. Clearly the expression is a string. The find compares the current total to the goal, and if it's equal then the expression is correct and it returns it, if the current total is > of the goal then he failed and return null. Otherwhise he add some operations to the expression. He tries two "ways", one multiplying * 3 the current result, the other adding +5 to the current result. It's recursive on a tree (so each time he will bifurcate in two recursive calls). null || something == something, so the branch that will find a response will return his response, the other branch will return null and the "winning" response will be passed back.
compares 11 with 11. The numbers are equal. So he returns "((1 + 5) + 5)" (the history). 5, 6, 7 will at a certain point go "overboard" and surpass the 11, so they'll return null. null || null == null, "((1 + 5) + 5)" || null == "((1 + 5) + 5)", so the history will win against the nulls and it will be returned.
To make it even clearer, try these versions:
function findSequence(goal) {
function find(start, history) {
if (start == goal)
return history;
else if (start > goal)
return null;
else {
var ret = find(start + 5, "(" + history + " + 5)");
if (ret == null)
ret = find(start * 3, "(" + history + " * 3)");
return ret;
}
}
return find(1, "1");
}
print(findSequence(24));
And this, where instead of an expression you'll get only as tring of + and *
function findSequence(goal) {
function find(start, history) {
if (start == goal)
return history;
else if (start > goal)
return null;
else {
var ret = find(start + 5, history + "+");
if (ret == null)
ret = find(start * 3, history + "*");
return ret;
}
}
return find(1, "1");
}
print(findSequence(24));
And be aware that, as an example, it's quite complex because it used closures (locally defined functions).
发布评论
评论(1)
您有一个“输出”字符串
(((1 * 3) + 5) * 3)
、目标24
和第一个字符1
。1
是1 * 3
中的 1。该程序将组装一个包含数学表达式的字符串,添加 ()、*3 和 +5 以尝试获得目标。 find 函数的两个参数是当前总计和将生成总计的表达式。显然表达式是一个字符串。 find 将当前总数与目标进行比较,如果相等,则表达式正确,并且如果当前总数 > 则返回它。目标失败,然后返回 null。否则他会向表达式添加一些操作。他尝试了两种“方法”,一种将当前结果乘以 * 3,另一种将当前结果加 +5。它在一棵树上递归(所以每次他都会分叉两次递归调用)。空 || Something == Something,因此找到响应的分支将返回他的响应,另一个分支将返回 null,并且“获胜”响应将被传回。假设目标是 11.find
为了使其更清楚,请尝试以下版本
: where 而不是表达式,您只能得到 + 和 * 的字符串,
并且请注意,作为示例,它非常复杂,因为它使用了闭包(本地定义的函数)。
You have an "output" string
(((1 * 3) + 5) * 3)
, a goal24
and a first character1
. The1
is the 1 in1 * 3
. This program will assemble a string containing a math expression, adding (), *3 and +5 to try to obtain the goal. The two parameters of thefind
function are the current total and the expression that will generate the total. Clearly the expression is a string. The find compares the current total to the goal, and if it's equal then the expression is correct and it returns it, if the current total is > of the goal then he failed and return null. Otherwhise he add some operations to the expression. He tries two "ways", one multiplying * 3 the current result, the other adding +5 to the current result. It's recursive on a tree (so each time he will bifurcate in two recursive calls). null || something == something, so the branch that will find a response will return his response, the other branch will return null and the "winning" response will be passed back.Let's say the goal is 11.
To make it even clearer, try these versions:
And this, where instead of an expression you'll get only as tring of + and *
And be aware that, as an example, it's quite complex because it used closures (locally defined functions).