chore(i18n,learn): processed translations (#44851)
This commit is contained in:
@ -0,0 +1,150 @@
|
||||
---
|
||||
id: a3f503de51cf954ede28891d
|
||||
title: 対称差を見つける
|
||||
challengeType: 5
|
||||
forumTopicId: 301611
|
||||
dashedName: find-the-symmetric-difference
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
2 つの集合の「<dfn>対称差</dfn>」(`△` または `⊕`) という数学用語は、2 つの集合のいずれかにあるが両方にはない要素の集合を意味します。 例えば、集合 `A = {1, 2, 3}` と集合 `B = {2, 3, 4}`の場合、`A △ B = {1, 4}` です。
|
||||
|
||||
対称差は二項演算で、すなわち 2 つの要素のみに対する演算です。 そのため、*3 つ*の要素間 (`A △ B △ C`) の対称差を求める式を評価するには、一度に 1 つずつ演算を完了させなければなりません。 したがって、上記の集合 `A` と `B` に加えて `C = {2, 3}` がある場合、`A △ B △ C = (A △ B) △ C = {1, 4} △ {2, 3} = {1, 2, 3, 4}` です。
|
||||
|
||||
# --instructions--
|
||||
|
||||
2 つ以上の配列を取りそれらの対称差の配列を返す、1 つの関数を作成してください。 返される配列には一意の値のみ (*重複なし*) が含まれている必要があります。
|
||||
|
||||
# --hints--
|
||||
|
||||
`sym([1, 2, 3], [5, 2, 1, 4])` は `[3, 4, 5]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5]);
|
||||
```
|
||||
|
||||
`sym([1, 2, 3], [5, 2, 1, 4])` には 3 つの要素のみが含まれている必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3);
|
||||
```
|
||||
|
||||
`sym([1, 2, 3, 3], [5, 2, 1, 4])` は `[3, 4, 5]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5]);
|
||||
```
|
||||
|
||||
`sym([1, 2, 3, 3], [5, 2, 1, 4])` には 3 つの要素のみが含まれている必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3);
|
||||
```
|
||||
|
||||
`sym([1, 2, 3], [5, 2, 1, 4, 5])` は `[3, 4, 5]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5]);
|
||||
```
|
||||
|
||||
`sym([1, 2, 3], [5, 2, 1, 4, 5])` には 3 つの要素のみが含まれている必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3);
|
||||
```
|
||||
|
||||
`sym([1, 2, 5], [2, 3, 5], [3, 4, 5])` は `[1, 4, 5]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5]);
|
||||
```
|
||||
|
||||
`sym([1, 2, 5], [2, 3, 5], [3, 4, 5])` には 3 つの要素のみが含まれている必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3);
|
||||
```
|
||||
|
||||
`sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])` は `[1, 4, 5]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5]);
|
||||
```
|
||||
|
||||
`sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])` には 3 つの要素のみが含まれている必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3);
|
||||
```
|
||||
|
||||
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])` は `[2, 3, 4, 6, 7]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]),
|
||||
[2, 3, 4, 6, 7]
|
||||
);
|
||||
```
|
||||
|
||||
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])` には 5 つの要素のみが含まれている必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length,
|
||||
5
|
||||
);
|
||||
```
|
||||
|
||||
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])` は `[1, 2, 4, 5, 6, 7, 8, 9]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
sym(
|
||||
[3, 3, 3, 2, 5],
|
||||
[2, 1, 5, 7],
|
||||
[3, 4, 6, 6],
|
||||
[1, 2, 3],
|
||||
[5, 3, 9, 8],
|
||||
[1]
|
||||
),
|
||||
[1, 2, 4, 5, 6, 7, 8, 9]
|
||||
);
|
||||
```
|
||||
|
||||
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])` には 8 つの要素のみが含まれている必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])
|
||||
.length,
|
||||
8
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sym(args) {
|
||||
return args;
|
||||
}
|
||||
|
||||
sym([1, 2, 3], [5, 2, 1, 4]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sym() {
|
||||
var arrays = [].slice.call(arguments);
|
||||
return arrays.reduce(function (symDiff, arr) {
|
||||
return symDiff.concat(arr).filter(function (val, idx, theArr) {
|
||||
return theArr.indexOf(val) === idx
|
||||
&& (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);
|
||||
});
|
||||
});
|
||||
}
|
||||
sym([1, 2, 3], [5, 2, 1, 4]);
|
||||
```
|
@ -0,0 +1,150 @@
|
||||
---
|
||||
id: 61abc7ebf3029b56226de5b6
|
||||
title: 二分探索を実装する
|
||||
challengeType: 1
|
||||
forumTopicId: 487618
|
||||
dashedName: implement-binary-search
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
二分探索はソートされた配列内で要素を検索するアルゴリズムで、その時間計算量は **O(log(n))** です。 次のように演算が行われます。
|
||||
|
||||
1. ソートされた配列内の中央の `value` (値) を探します。 `value == target` であれば、「見つけた!」 を返します。
|
||||
1. 中央の `value < target` であれば、次の比較で配列の右半分を検索します。
|
||||
1. 中央の `value > target` であれば、次の比較で配列の左半分を検索します。
|
||||
|
||||
このように配列を連続的に半減させており、時間計算量は log(n) になります。 このチャレンジでは、あなたがどのような経路をたどって目標値に到達したかを提示していただきます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
二分探索アルゴリズムを配列に実装し、配列内で目標値を見つけるためにたどった経路 (各回の中間値比較) を返すような関数 `binarySearch` を記述してください。
|
||||
|
||||
この関数は、ソートされた整数配列と目標値を入力として取ります。 そして目標値を見つけるまで、元の配列の各 2 分割で見つけた中間値が含まれる配列を返します (通りがけ順)。 目標値は、返される配列の最後の要素でなければなりません。 値が見つからない場合は、文字列 `Value Not Found` (値が見つかりません) を返します。
|
||||
|
||||
例えば、`binarySearch([1,2,3,4,5,6,7], 5)` は `[4,6,5]` を返します。
|
||||
|
||||
このチャレンジでは、2 分割する際、除算に `Math.floor()` を必ず使用して `Math.floor(x/2)` としなければなりません。 これにより、一貫性のある検証可能な経路が得られます。
|
||||
|
||||
**注:** 検証では以下の配列が使用されます。
|
||||
|
||||
```js
|
||||
const testArray = [
|
||||
0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
|
||||
23, 49, 70
|
||||
];
|
||||
```
|
||||
|
||||
# --hints--
|
||||
|
||||
`binarySearch` は関数でなければなりません。
|
||||
|
||||
```js
|
||||
assert(typeof binarySearch == 'function');
|
||||
```
|
||||
|
||||
`binarySearch(testArray, 0)` は `[13, 5, 2, 0]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(binarySearch(_testArray, 0), [13, 5, 2, 0]);
|
||||
```
|
||||
|
||||
`binarySearch(testArray, 1)` は `[13, 5, 2, 0, 1]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(binarySearch(_testArray, 1), [13, 5, 2, 0, 1]);
|
||||
```
|
||||
|
||||
|
||||
`binarySearch(testArray, 2)` は `[13, 5, 2]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(binarySearch(_testArray, 2), [13, 5, 2]);
|
||||
```
|
||||
|
||||
`binarySearch(testArray, 6)` は文字列 `Value Not Found` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.strictEqual(binarySearch(_testArray, 6), 'Value Not Found');
|
||||
```
|
||||
|
||||
`binarySearch(testArray, 11)` は `[13, 5, 10, 11]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(binarySearch(_testArray, 11), [13, 5, 10, 11])
|
||||
```
|
||||
|
||||
`binarySearch(testArray, 13)` は `[13]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(binarySearch(_testArray, 13), [13]);
|
||||
```
|
||||
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
const _testArray = [
|
||||
0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
|
||||
23, 49, 70
|
||||
];
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function binarySearch(searchList, value) {
|
||||
let arrayPath = [];
|
||||
return arrayPath;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let binarySearch = (searchList, value) => {
|
||||
let arrayPath = [];
|
||||
|
||||
// set initial L - M - R
|
||||
let left = 0;
|
||||
let right = searchList.length - 1;
|
||||
let middle = Math.floor(right / 2);
|
||||
|
||||
// if first comparison finds value
|
||||
if (searchList[middle] == value) {
|
||||
arrayPath.push(searchList[middle]);
|
||||
return arrayPath;
|
||||
}
|
||||
|
||||
while (searchList[middle] !== value) {
|
||||
// add to output array
|
||||
arrayPath.push(searchList[middle]);
|
||||
|
||||
// not found
|
||||
if (right < left) {
|
||||
return 'Value Not Found';
|
||||
}
|
||||
// value is in left or right portion of array
|
||||
// update L - M - R
|
||||
if (searchList[middle] > value) {
|
||||
right = middle - 1;
|
||||
middle = left + Math.floor((right - left) / 2);
|
||||
} else {
|
||||
left = middle + 1;
|
||||
middle = left + Math.floor((right - left) / 2);
|
||||
}
|
||||
|
||||
// if found update output array and exit
|
||||
if (searchList[middle] == value) {
|
||||
arrayPath.push(searchList[middle]);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
return arrayPath;
|
||||
};
|
||||
```
|
@ -0,0 +1,138 @@
|
||||
---
|
||||
id: 8d5123c8c441eddfaeb5bdef
|
||||
title: バブルソートを実装する
|
||||
challengeType: 1
|
||||
forumTopicId: 301612
|
||||
dashedName: implement-bubble-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
このチャレンジは、ソートアルゴリズムに関するいくつかのチャレンジの 1 つ目です。 ソートされていない要素の配列が与えられ、ソートされた配列を返す操作を行います。 これを行うための方法をいくつか知り、それらの異なるアプローチの間にどのようなトレードオフがあるかを学びます。 最近のほとんどの言語にはこのような操作を行うソート方法が組み込まれていますが、一般的な基本アプローチをいくつか理解し、それらの実装方法を学ぶことはやはり重要です。
|
||||
|
||||
まずはバブルソートです。 バブルソート法は、ソートされていない配列の先頭から始まり、ソートされていない値を末尾に向けて押し出す「バブルアップ」を行います。これを、配列が完全にソートされるまで繰り返します。 具体的には、隣接する要素を比較し、順序が正しくなければそれらを交換します。 配列がソートされて交換が発生しなくなるまで、この操作が配列内をループし続けます。
|
||||
|
||||
この方法では配列全体にわたり操作が何度も繰り返されることになり、平均ケースおよび最悪ケースで二乗時間計算量になります。 単純ですが、ほとんどの状況では一般に実用的ではありません。
|
||||
|
||||
**手順:** 整数の配列を入力として受け取り、それらの整数の配列を最小から最大の順にソートして返す、`bubbleSort` 関数を記述してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`bubbleSort` は関数でなければなりません。
|
||||
|
||||
```js
|
||||
assert(typeof bubbleSort == 'function');
|
||||
```
|
||||
|
||||
`bubbleSort` はソートされた配列を返す必要があります (最小から最大の順)。
|
||||
|
||||
```js
|
||||
assert(
|
||||
isSorted(
|
||||
bubbleSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])` は、順序以外は変更されていない配列を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
bubbleSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
]),
|
||||
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
);
|
||||
```
|
||||
|
||||
`bubbleSort` には組み込みの `.sort()` メソッドを使用しないでください。
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
bubbleSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function bubbleSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function bubbleSort(array) {
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
let swapped = false;
|
||||
for (let j = 1; j < array.length; j++) {
|
||||
if (array[j - 1] > array[j]) {
|
||||
let temp = array[j-1];
|
||||
array[j-1] = array[j];
|
||||
array[j] = temp;
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
if (swapped === false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
```
|
@ -0,0 +1,135 @@
|
||||
---
|
||||
id: 587d8259367417b2b2512c86
|
||||
title: 挿入ソートを実装する
|
||||
challengeType: 1
|
||||
forumTopicId: 301613
|
||||
dashedName: implement-insertion-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
次のソート方法は挿入ソートです。 この方法は、ソート済みの配列をリストの先頭に作ることで機能します。 まず、1 つ目の要素を使って、ソート済みの配列を作り始めます。 そして次の要素を調べ、それが正しいソート位置に入るまで、ソート済みの配列の中で後ろの要素と交換していきます。 リスト全体を通してそれを繰り返し、末尾に到達するまで新しい要素を後方に置き換え続けます。 このアルゴリズムは、平均ケースおよび最悪ケースで二乗時間計算量になります。
|
||||
|
||||
**手順:** 整数の配列を入力として受け取り、それらの整数の配列を最小から最大の順にソートして返す、`insertionSort` 関数を記述してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`insertionSort` は関数でなければなりません。
|
||||
|
||||
```js
|
||||
assert(typeof insertionSort == 'function');
|
||||
```
|
||||
|
||||
`insertionSort` はソートされた配列を返す必要があります (最小から最大の順)。
|
||||
|
||||
```js
|
||||
assert(
|
||||
isSorted(
|
||||
insertionSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])` は、順序以外は変更されていない配列を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
insertionSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
]),
|
||||
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
);
|
||||
```
|
||||
|
||||
`insertionSort([5, 4, 33, 2, 8])` は `[2, 4, 5, 8, 33]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(insertionSort([5, 4, 33, 2, 8]), [2, 4, 5, 8, 33])
|
||||
```
|
||||
|
||||
`insertionSort` には組み込みの `.sort()` メソッドを使用しないでください。
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
insertionSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function insertionSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function insertionSort (array) {
|
||||
for (let currentIndex = 0; currentIndex < array.length; currentIndex++) {
|
||||
let current = array[currentIndex];
|
||||
let j = currentIndex - 1;
|
||||
while (j > -1 && array[j] > current) {
|
||||
array[j + 1] = array[j];
|
||||
j--;
|
||||
}
|
||||
array[j + 1] = current;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
```
|
@ -0,0 +1,156 @@
|
||||
---
|
||||
id: 587d825c367417b2b2512c8f
|
||||
title: マージソートを実装する
|
||||
challengeType: 1
|
||||
forumTopicId: 301614
|
||||
dashedName: implement-merge-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
もう一つの一般的な中間ソートアルゴリズムはマージソートです。 クイックソートと同様に、マージソートは再帰的な分割統治法を使用して配列をソートします。 この方法は、最初からそれぞれがソートされている 2 つの配列をソートするのは比較的簡単であるという事実を利用しています。 しかし、まずは入力として配列を 1 つだけ使いましょう。さて、そこから 2 つのソート済み配列をどのように取得するのでしょうか? 要素を 1 つのみ持つ配列という初期条件に到達するまで、元の入力を再帰的に 2 分割すれば良いのです。 単一要素の配列は当然ソート済みなので、次に合体を始めることができます。 この合体により、元の配列を分割する再帰呼び出しが巻き戻され、最後にはすべての要素を持つ最終的なソート済み配列が生成されます。 マージソートの手順は次のとおりです。
|
||||
|
||||
**1)** 要素を 1 つのみ持つ部分配列が生成されるまで、入力配列を再帰的に 2 分割します。
|
||||
|
||||
**2)** ソート済みの各部分配列をマージして最終的なソート済み配列を生成します。
|
||||
|
||||
マージソートは効率的なソート方法で、時間計算量は *O(nlog(n))* です。 このアルゴリズムはパフォーマンスが高く実装が比較的容易であるため、広く使用されます。
|
||||
|
||||
余談ですが、これはここで取り扱う最後のソートアルゴリズムです。 ただしツリーデータ構造のセクションの後半で、ヒープソートについて説明します。これも効率的なソート方法であり、その実装には二分ヒープが必要です。
|
||||
|
||||
**手順:** 整数の配列を入力として受け取り、それらの整数の配列を最小から最大の順にソートして返す、`mergeSort` 関数を記述してください。 これを実装する良い方法は、1 つの関数、例えば `merge` (ソートされた 2 つの配列をマージする関数) と、別の 1 つの関数、例えば `mergeSort` (merge 関数の入力となる単一要素配列を生成するために再帰的操作を行う関数) を書くことです。 頑張ってください!
|
||||
|
||||
# --hints--
|
||||
|
||||
`mergeSort` は関数でなければなりません。
|
||||
|
||||
```js
|
||||
assert(typeof mergeSort == 'function');
|
||||
```
|
||||
|
||||
`mergeSort` はソートされた配列を返す必要があります (最小から最大の順)。
|
||||
|
||||
```js
|
||||
assert(
|
||||
isSorted(
|
||||
mergeSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])` は、順序以外は変更されていない配列を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
mergeSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
]),
|
||||
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
);
|
||||
```
|
||||
|
||||
`mergeSort` には組み込みの `.sort()` メソッドを使用しないでください。
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
mergeSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
if (array.length === 1) {
|
||||
return array;
|
||||
} else {
|
||||
const splitIndex = Math.floor(array.length / 2);
|
||||
return merge(
|
||||
mergeSort(array.slice(0, splitIndex)),
|
||||
mergeSort(array.slice(splitIndex))
|
||||
);
|
||||
}
|
||||
|
||||
// Merge two sorted arrays
|
||||
function merge(array1, array2) {
|
||||
let merged = [];
|
||||
while (array1.length && array2.length) {
|
||||
if (array1[0] < array2[0]) {
|
||||
merged.push(array1.shift());
|
||||
} else if (array1[0] > array2[0]) {
|
||||
merged.push(array2.shift());
|
||||
} else {
|
||||
merged.push(array1.shift(), array2.shift());
|
||||
}
|
||||
}
|
||||
|
||||
// After looping ends, one array is empty, and other array contains only
|
||||
// values greater than all values in `merged`
|
||||
return [...merged, ...array1, ...array2];
|
||||
}
|
||||
}
|
||||
|
||||
mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
@ -0,0 +1,142 @@
|
||||
---
|
||||
id: 587d825a367417b2b2512c89
|
||||
title: クイックソートを実装する
|
||||
challengeType: 1
|
||||
forumTopicId: 301615
|
||||
dashedName: implement-quick-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
次は、中間ソートアルゴリズムであるクイックソートです。 クイックソートは、配列を効率的にソートするための再帰的な分割統治法です。 この方法では、元の配列でピボット値が選択されます。 次に、配列は、ピボット値よりも小さい値と大きい値の 2 つの部分配列に分割されます。 次に、両方の部分配列でクイックソートアルゴリズムを再帰的に呼び出した結果を組み合わせます。 これは、空の配列または単一要素の配列という初期条件に達するまで続き、私たちはそれを返します。 再帰呼び出しを巻き戻すと、ソートされた配列が返されます。
|
||||
|
||||
クイックソートは非常に効率的なソート方法で、平均で *O(nlog(n))* のパフォーマンスを提供します。 また、実装も比較的容易です。 これらの特性を持つため、クイックソートはよく使用される便利なソート方法です。
|
||||
|
||||
**手順:** 整数の配列を入力として受け取り、それらの整数の配列を最小から最大の順にソートして返す、`quickSort` 関数を記述してください。 ピボット値の選択は重要ですが、ここでの目的上、どのようなピボットでも構いません。 単純化するために、最初または最後の要素を使用することもできます。
|
||||
|
||||
# --hints--
|
||||
|
||||
`quickSort` は関数でなければなりません。
|
||||
|
||||
```js
|
||||
assert(typeof quickSort == 'function');
|
||||
```
|
||||
|
||||
`quickSort` はソートされた配列を返す必要があります (最小から最大の順)。
|
||||
|
||||
```js
|
||||
assert(
|
||||
isSorted(
|
||||
quickSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])` は、順序以外は変更されていない配列を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
quickSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
]),
|
||||
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
);
|
||||
```
|
||||
|
||||
`quickSort` には組み込みの `.sort()` メソッドを使用しないでください。
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
quickSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function quickSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function quickSort(array) {
|
||||
if (array.length === 0) {
|
||||
return [];
|
||||
} else {
|
||||
const pivotValue = array[0];
|
||||
|
||||
// Sort elements into three piles
|
||||
let lesser = [];
|
||||
let equal = [];
|
||||
let greater = [];
|
||||
for (let e of array) {
|
||||
if (e < pivotValue) {
|
||||
lesser.push(e);
|
||||
} else if (e > pivotValue) {
|
||||
greater.push(e);
|
||||
} else {
|
||||
equal.push(e);
|
||||
}
|
||||
}
|
||||
|
||||
return [...quickSort(lesser), ...equal, ...quickSort(greater)];
|
||||
}
|
||||
}
|
||||
```
|
@ -0,0 +1,131 @@
|
||||
---
|
||||
id: 587d8259367417b2b2512c85
|
||||
title: 選択ソートを実装する
|
||||
challengeType: 1
|
||||
forumTopicId: 301616
|
||||
dashedName: implement-selection-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
ここでは選択ソートを実装します。 選択ソートは、まずリスト内の最小値を選択し、それをリスト内の最初の値と交換します。 次に、2 番目の位置から始め、残りのリスト内の最小値を選択し、2 番目の要素と交換します。 リストの最後に到達するまで、リスト全体で要素の入れ替えを繰り返します。 これで、リストがソートされます。 選択ソートは、どのケースでも二乗時間計算量です。
|
||||
|
||||
**手順**: 整数の配列を入力として受け取り、それらの整数の配列を最小から最大の順にソートして返す、`selectionSort` 関数を記述してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`selectionSort` は関数でなければなりません。
|
||||
|
||||
```js
|
||||
assert(typeof selectionSort == 'function');
|
||||
```
|
||||
|
||||
`selectionSort` はソートされた配列を返す必要があります (最小から最大の順)。
|
||||
|
||||
```js
|
||||
assert(
|
||||
isSorted(
|
||||
selectionSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])` は、順序以外は変更されていない配列を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
selectionSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
]),
|
||||
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
);
|
||||
```
|
||||
|
||||
`selectionSort` には組み込みの `.sort()` メソッドを使用しないでください。
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
selectionSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function selectionSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function selectionSort(array) {
|
||||
for (let i = 0; i < array.length-1; i++) {
|
||||
let minimumIndex = i;
|
||||
for (let j = i+1; j < array.length; j++){
|
||||
if (array[j] < array[minimumIndex]) {
|
||||
minimumIndex = j;
|
||||
}
|
||||
}
|
||||
let value = array[minimumIndex];
|
||||
array[minimumIndex] = array[i];
|
||||
array[i] = value;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
```
|
@ -0,0 +1,228 @@
|
||||
---
|
||||
id: a56138aff60341a09ed6c480
|
||||
title: 在庫を更新する
|
||||
challengeType: 5
|
||||
forumTopicId: 16019
|
||||
dashedName: inventory-update
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
二次元配列に格納されている在庫を、フレッシュデリバリーの 2 つ目の二次元配列と比較して更新します。 既存の在庫品目の数量 (`arr1` 内) を更新します。 品目が見つからない場合は、新しい品目と数量を在庫配列に追加します。 返された在庫配列は、品目名でアルファベット順にする必要があります。
|
||||
|
||||
# --hints--
|
||||
|
||||
関数 `updateInventory` は配列を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.isArray(
|
||||
updateInventory(
|
||||
[
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
],
|
||||
[
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[67, 'Bowling Ball'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])` は長さ 6 の配列を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
updateInventory(
|
||||
[
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
],
|
||||
[
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[67, 'Bowling Ball'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
).length,
|
||||
6
|
||||
);
|
||||
```
|
||||
|
||||
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])` は `[[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
updateInventory(
|
||||
[
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
],
|
||||
[
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[67, 'Bowling Ball'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
),
|
||||
[
|
||||
[88, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[3, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[5, 'Microphone'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])` は `[[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
updateInventory(
|
||||
[
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
],
|
||||
[]
|
||||
),
|
||||
[
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])` は `[[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
updateInventory(
|
||||
[],
|
||||
[
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[67, 'Bowling Ball'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
),
|
||||
[
|
||||
[67, 'Bowling Ball'],
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]])` は `[[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
updateInventory(
|
||||
[
|
||||
[0, 'Bowling Ball'],
|
||||
[0, 'Dirty Sock'],
|
||||
[0, 'Hair Pin'],
|
||||
[0, 'Microphone']
|
||||
],
|
||||
[
|
||||
[1, 'Hair Pin'],
|
||||
[1, 'Half-Eaten Apple'],
|
||||
[1, 'Bowling Ball'],
|
||||
[1, 'Toothpaste']
|
||||
]
|
||||
),
|
||||
[
|
||||
[1, 'Bowling Ball'],
|
||||
[0, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[1, 'Half-Eaten Apple'],
|
||||
[0, 'Microphone'],
|
||||
[1, 'Toothpaste']
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function updateInventory(arr1, arr2) {
|
||||
return arr1;
|
||||
}
|
||||
|
||||
// Example inventory lists
|
||||
var curInv = [
|
||||
[21, "Bowling Ball"],
|
||||
[2, "Dirty Sock"],
|
||||
[1, "Hair Pin"],
|
||||
[5, "Microphone"]
|
||||
];
|
||||
|
||||
var newInv = [
|
||||
[2, "Hair Pin"],
|
||||
[3, "Half-Eaten Apple"],
|
||||
[67, "Bowling Ball"],
|
||||
[7, "Toothpaste"]
|
||||
];
|
||||
|
||||
updateInventory(curInv, newInv);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function updateInventory(arr1, arr2) {
|
||||
arr2.forEach(function(item) {
|
||||
createOrUpdate(arr1, item);
|
||||
});
|
||||
// All inventory must be accounted for or you're fired!
|
||||
return arr1;
|
||||
}
|
||||
|
||||
function createOrUpdate(arr1, item) {
|
||||
var index = -1;
|
||||
while (++index < arr1.length) {
|
||||
if (arr1[index][1] === item[1]) {
|
||||
arr1[index][0] += item[0];
|
||||
return;
|
||||
}
|
||||
if (arr1[index][1] > item[1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
arr1.splice(index, 0, item);
|
||||
}
|
||||
|
||||
// Example inventory lists
|
||||
var curInv = [
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
];
|
||||
|
||||
var newInv = [
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[67, 'Bowling Ball'],
|
||||
[7, 'Toothpaste']
|
||||
];
|
||||
|
||||
updateInventory(curInv, newInv);
|
||||
```
|
@ -0,0 +1,126 @@
|
||||
---
|
||||
id: a7bf700cd123b9a54eef01d5
|
||||
title: 繰り返しなし
|
||||
challengeType: 5
|
||||
forumTopicId: 16037
|
||||
dashedName: no-repeats-please
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
与えられた文字列の全順列のうち、同じ文字の連続的な繰り返しを含まない順列がいくつあるかを返します。 与えられた文字列のすべての文字がそれぞれ一意であると仮定します。
|
||||
|
||||
例えば `aab` は、計 6 つの順列 (`aab`, `aab`, `aba`, `aba`, `baa`, `baa`) を持ちますが、そのうち 2 つ (`aba` と `aba`) だけは同じ文字 (この場合は `a`) が繰り返されていないので、2 を返す必要があります。
|
||||
|
||||
# --hints--
|
||||
|
||||
`permAlone("aab")` は数値を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.isNumber(permAlone('aab'));
|
||||
```
|
||||
|
||||
`permAlone("aab")` は 2 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aab'), 2);
|
||||
```
|
||||
|
||||
`permAlone("aaa")` は 0 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aaa'), 0);
|
||||
```
|
||||
|
||||
`permAlone("aabb")` は 8 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aabb'), 8);
|
||||
```
|
||||
|
||||
`permAlone("abcdefa")` は 3600 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('abcdefa'), 3600);
|
||||
```
|
||||
|
||||
`permAlone("abfdefa")` は 2640 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('abfdefa'), 2640);
|
||||
```
|
||||
|
||||
`permAlone("zzzzzzzz")` は 0 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('zzzzzzzz'), 0);
|
||||
```
|
||||
|
||||
`permAlone("a")` は 1 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('a'), 1);
|
||||
```
|
||||
|
||||
`permAlone("aaab")` は 0 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aaab'), 0);
|
||||
```
|
||||
|
||||
`permAlone("aaabb")` は 12 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aaabb'), 12);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function permAlone(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
permAlone('aab');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function permAlone(str) {
|
||||
return permuter(str).filter(function(perm) {
|
||||
return !perm.match(/(.)\1/g);
|
||||
}).length;
|
||||
}
|
||||
|
||||
function permuter(str) {
|
||||
// http://staff.roguecc.edu/JMiller/JavaScript/permute.html
|
||||
//permArr: Global array which holds the list of permutations
|
||||
//usedChars: Global utility array which holds a list of "currently-in-use" characters
|
||||
var permArr = [], usedChars = [];
|
||||
function permute(input) {
|
||||
//convert input into a char array (one element for each character)
|
||||
var i, ch, chars = input.split("");
|
||||
for (i = 0; i < chars.length; i++) {
|
||||
//get and remove character at index "i" from char array
|
||||
ch = chars.splice(i, 1);
|
||||
//add removed character to the end of used characters
|
||||
usedChars.push(ch);
|
||||
//when there are no more characters left in char array to add, add used chars to list of permutations
|
||||
if (chars.length === 0) permArr[permArr.length] = usedChars.join("");
|
||||
//send characters (minus the removed one from above) from char array to be permuted
|
||||
permute(chars.join(""));
|
||||
//add removed character back into char array in original position
|
||||
chars.splice(i, 0, ch);
|
||||
//remove the last character used off the end of used characters array
|
||||
usedChars.pop();
|
||||
}
|
||||
}
|
||||
permute(str);
|
||||
return permArr;
|
||||
}
|
||||
|
||||
permAlone('aab');
|
||||
```
|
@ -0,0 +1,100 @@
|
||||
---
|
||||
id: a3f503de51cfab748ff001aa
|
||||
title: ペアワイズ
|
||||
challengeType: 5
|
||||
forumTopicId: 301617
|
||||
dashedName: pairwise
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
配列 `arr` について、和が 2 つ目の引数 `arg` に等しくなる要素ペアを見つけ、それらのインデックスの和を返します。
|
||||
|
||||
数値要素は同じだがインデックスが異なる、複数のペアを使用できます。 各ペアは、利用可能な最低のインデックスを使用する必要があります。 一度要素を使用すると、それを他の要素とペアリングすることはできません。 例えば、`pairwise([1, 1, 2], 3)`は、インデックス 1 にある 1 ではなく、インデックス 0 にある 1 を使って、ペア `[2, 1]` を作成します。なぜなら、0+2 < 1+2 だからです。
|
||||
|
||||
例えば、`pairwise([7, 9, 11, 13, 15], 20)` は `6` を返します。 和が 20 であるペアは `[7, 13]` と `[9, 11]` です。 次に、それらのインデックスと値を使って配列を書き出すことができます。
|
||||
|
||||
<div style='margin-left: 2em;'>
|
||||
|
||||
| インデックス | 0 | 1 | 2 | 3 | 4 |
|
||||
| ----- | - | - | -- | -- | -- |
|
||||
| 値 | 7 | 9 | 11 | 13 | 15 |
|
||||
|
||||
</div>
|
||||
|
||||
以下では、対応するインデックスを取り、追加します。
|
||||
|
||||
<div style='margin-left: 2em;'>
|
||||
|
||||
7 + 13 = 20 → インデックス 0 + 3 = 3
|
||||
9 + 11 = 20 → インデックス 1 + 2 = 3
|
||||
3 + 3 = 6 → `6` を返す
|
||||
|
||||
</div>
|
||||
|
||||
# --hints--
|
||||
|
||||
`pairwise([1, 4, 2, 3, 0, 5], 7)` は 11 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11);
|
||||
```
|
||||
|
||||
`pairwise([1, 3, 2, 4], 4)` は 1 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1);
|
||||
```
|
||||
|
||||
`pairwise([1, 1, 1], 2)` は 1 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([1, 1, 1], 2), 1);
|
||||
```
|
||||
|
||||
`pairwise([0, 0, 0, 0, 1, 1], 1)` は 10 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10);
|
||||
```
|
||||
|
||||
`pairwise([], 100)` は 0 を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([], 100), 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pairwise(arr, arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
pairwise([1,4,2,3,0,5], 7);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
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);
|
||||
```
|
Reference in New Issue
Block a user