* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.1 KiB
2.1 KiB
id, title, challengeType, videoUrl, dashedName
id | title | challengeType | videoUrl | dashedName |
---|---|---|---|---|
a3f503de51cfab748ff001aa | 成对 | 5 | pairwise |
--description--
给定一个数组arr
,找到其总和等于第二个参数arg
元素对,并返回它们的索引之和。您可以使用具有相同数字元素但索引不同的多个对。每对应使用尽可能低的可用指数。一旦元素被使用,它就不能被重用来与另一个元素配对。例如, pairwise([1, 1, 2], 3)
使用indice 0处的1而不是indice 1处的1创建一对[2, 1]
,因为0 + 2 <1 + 2。例如, pairwise([7, 9, 11, 13, 15], 20)
返回6
。总和为20的对是[7, 13]
和[9, 11]
。然后我们可以用它们的索引和值写出数组。
指数 | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
值 | 7 | 9 | 11 | 13 | 15 |
下面我们将采用相应的索引并添加它们。 7 + 13 = 20→指数0 + 3 = 3
9 + 11 = 20→指数1 + 2 = 3
3 + 3 = 6→返回6
如果卡住,请记住使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
--hints--
pairwise([1, 4, 2, 3, 0, 5], 7)
应该返回11。
assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11);
pairwise([1, 3, 2, 4], 4)
应该返回1。
assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1);
pairwise([1, 1, 1], 2)
应该返回1。
assert.deepEqual(pairwise([1, 1, 1], 2), 1);
pairwise([0, 0, 0, 0, 1, 1], 1)
应返回10。
assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10);
pairwise([], 100)
应该返回0。
assert.deepEqual(pairwise([], 100), 0);
--seed--
--seed-contents--
function pairwise(arr, arg) {
return arg;
}
pairwise([1,4,2,3,0,5], 7);
--solutions--
function pairwise(arr, arg) {
var sum = 0;
arr.forEach(function(e, i, a) {
if (e != null) {
var diff = arg-e;
a[i] = null;
var dix = a.indexOf(diff);
if (dix !== -1) {
sum += dix;
sum += i;
a[dix] = null;
}
}
});
return sum;
}
pairwise([1,4,2,3,0,5], 7);