* 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.5 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
5a23c84252665b21eecc802a | Stream Merge | 5 | 302326 | stream-merge |
--description--
Write a function that takes multiple sorted arrays of items, and returns one array of sorted items.
--hints--
mergeLists
should be a function.
assert(typeof mergeLists == 'function');
mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]])
should return an array.
assert(
Array.isArray(
mergeLists([
[1, 3, 5, 9, 10],
[2, 4, 6, 7, 8]
])
)
);
mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]])
should return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.
assert.deepEqual(
mergeLists([
[1, 3, 5, 9, 10],
[2, 4, 6, 7, 8]
]),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
);
mergeLists([[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]])
should return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
.
assert.deepEqual(
mergeLists([
[1, 4, 7, 10],
[2, 5, 8, 11],
[3, 6, 9, 12]
]),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
);
mergeLists([[1, 3, 9, 14, 15, 17, 28], [7, 8, 14, 14, 23, 26, 28, 29, 30], [9, 23, 25, 29]])
should return [1, 3, 7, 8, 9, 9, 14, 14, 14, 15, 17, 23, 23, 25, 26, 28, 28, 29, 29, 30]
.
assert.deepEqual(
mergeLists([
[1, 3, 9, 14, 15, 17, 28],
[7, 8, 14, 14, 23, 26, 28, 29, 30],
[9, 23, 25, 29]
]),
[1, 3, 7, 8, 9, 9, 14, 14, 14, 15, 17, 23, 23, 25, 26, 28, 28, 29, 29, 30]
);
mergeLists([[3, 14, 15], [2, 17, 18], [], [2, 3, 5, 7]])
should return [2, 2, 3, 3, 5, 7, 14, 15, 17, 18]
.
assert.deepEqual(mergeLists([[3, 14, 15], [2, 17, 18], [], [2, 3, 5, 7]]), [
2,
2,
3,
3,
5,
7,
14,
15,
17,
18
]);
mergeLists([[1, 19, 1999], [17, 33, 2999, 3000], [8, 500, 3999]])
should return [1, 8, 17, 19, 33, 500, 1999, 2999, 3000, 3999]
.
assert.deepEqual(
mergeLists([
[1, 19, 1999],
[17, 33, 2999, 3000],
[8, 500, 3999]
]),
[1, 8, 17, 19, 33, 500, 1999, 2999, 3000, 3999]
);
--seed--
--seed-contents--
function mergeLists(lists) {
}
--solutions--
function mergeLists(lists) {
function merge(l1, l2) {
var result = [],
i = 0,
j = 0;
while (l1.length && l2.length) {
if (l1[i] <= l2[j]) {
result.push(l1.shift());
} else {
result.push(l2.shift());
}
}
result.push.apply(result, l1);
result.push.apply(result, l2);
return result;
}
var result = lists[0];
for (var i = 1; i < lists.length; i++) {
result = merge(result, lists[i]);
}
return result;
}