freeCodeCamp/curriculum/challenges/japanese/10-coding-interview-prep/rosetta-code/align-columns.md

190 lines
6.7 KiB
Markdown

---
id: 594810f028c0303b75339ad0
title: 列を揃える
challengeType: 1
forumTopicId: 302224
dashedName: align-columns
---
# --description--
たくさんの行を持つ配列で、行内のフィールドが単一の `$` 文字で区切られます。 各列の単語が少なくとも1つのスペースで区切られることで、フィールドの各列を揃えるプログラムを作成します。 さらに、各単語をその列内で左揃え、右揃え、または中央揃えにします。
# --instructions--
次のコードを使用して、プログラムをテストします。
```js
const testText = [
'Given$a$text$file$of$many$lines',
'where$fields$within$a$line$',
'are$delineated$by$a$single$"dollar"$character',
'write$a$program',
'that$aligns$each$column$of$fields',
'by$ensuring$that$words$in$each$',
'column$are$separated$by$at$least$one$space.',
'Further,$allow$for$each$word$in$a$column$to$be$either$left$',
'justified,$right$justified',
'or$center$justified$within$its$column.'
];
```
**注記:**
- 入力コードの例では、ドル文字を末尾に含めることもできますが、そうでない場合もあります。
- すべての列を同じように配列する必要があります。
- 行末に隣接して生成された連続空白文字は、タスクの目的において重要なものではありません。
- 出力テキストは、プレーンテキストエディタまたは基本端末に等幅フォントで表示されます。 その行は、改行文字 (`\n`) を使って結合します。
- 列間の最小スペースは、ハードコーディングせずに、テキストから計算します。
- 列間または列の周りに区切り文字を追加する必要はありません。
次の例は、 `testText`の行を右揃え、左揃え、中央揃えしたものです。
```js
' column are separated by at least one space.\n'
'column are separated by at least one space.\n'
' column are separated by at least one space.\n'
```
# --hints--
`formatText` という関数です。
```js
assert(typeof formatText === 'function');
```
`formatText(testText, 'right')` で、列が右揃えされたテキストが生成されます。
```js
assert.strictEqual(formatText(_testText, 'right'), rightAligned);
```
`formatText(testText, 'left')` で、列が左揃えされたテキストが生成されます。
```js
assert.strictEqual(formatText(_testText, 'left'), leftAligned);
```
`formatText(testText, 'center')` で、列が中央揃えされたテキストが生成されます。
```js
assert.strictEqual(formatText(_testText, 'center'), centerAligned);
```
# --seed--
## --after-user-code--
```js
const _testText = [
'Given$a$text$file$of$many$lines',
'where$fields$within$a$line$',
'are$delineated$by$a$single$"dollar"$character',
'write$a$program',
'that$aligns$each$column$of$fields$',
'by$ensuring$that$words$in$each$',
'column$are$separated$by$at$least$one$space.',
'Further,$allow$for$each$word$in$a$column$to$be$either$left$',
'justified,$right$justified',
'or$center$justified$within$its$column.'
];
const rightAligned = ' Given a text file of many lines\n' +
' where fields within a line \n' +
' are delineated by a single "dollar" character\n' +
' write a program\n' +
' that aligns each column of fields \n' +
' by ensuring that words in each \n' +
' column are separated by at least one space.\n' +
' Further, allow for each word in a column to be either left \n' +
'justified, right justified\n' +
' or center justified within its column.';
const leftAligned = 'Given a text file of many lines \n' +
'where fields within a line \n' +
'are delineated by a single "dollar" character\n' +
'write a program \n' +
'that aligns each column of fields \n' +
'by ensuring that words in each \n' +
'column are separated by at least one space.\n' +
'Further, allow for each word in a column to be either left \n' +
'justified, right justified\n' +
'or center justified within its column. ';
const centerAligned = ' Given a text file of many lines \n' +
' where fields within a line \n' +
' are delineated by a single \"dollar\" character\n' +
' write a program \n' +
' that aligns each column of fields \n' +
' by ensuring that words in each \n' +
' column are separated by at least one space.\n' +
' Further, allow for each word in a column to be either left \n' +
'justified, right justified\n' +
' or center justified within its column. ';
```
## --seed-contents--
```js
function formatText(input, justification) {
}
const testText = [
'Given$a$text$file$of$many$lines',
'where$fields$within$a$line$',
'are$delineated$by$a$single$"dollar"$character',
'write$a$program',
'that$aligns$each$column$of$fields$',
'by$ensuring$that$words$in$each$',
'column$are$separated$by$at$least$one$space.',
'Further,$allow$for$each$word$in$a$column$to$be$either$left$',
'justified,$right$justified',
'or$center$justified$within$its$column.'
];
```
# --solutions--
```js
String.prototype.repeat = function (n) { return new Array(1 + parseInt(n)).join(this); };
function formatText(input, justification) {
let x, y, max, cols = 0, diff, left, right;
for (x = 0; x < input.length; x++) {
input[x] = input[x].split('$');
if (input[x].length > cols) {
cols = input[x].length;
}
}
for (x = 0; x < cols; x++) {
max = 0;
for (y = 0; y < input.length; y++) {
if (input[y][x] && max < input[y][x].length) {
max = input[y][x].length;
}
}
for (y = 0; y < input.length; y++) {
if (input[y][x]) {
diff = (max - input[y][x].length) / 2;
left = ' '.repeat(Math.floor(diff));
right = ' '.repeat(Math.ceil(diff));
if (justification === 'left') {
right += left; left = '';
}
if (justification === 'right') {
left += right; right = '';
}
input[y][x] = left + input[y][x] + right;
}
}
}
for (x = 0; x < input.length; x++) {
input[x] = input[x].join(' ');
}
input = input.join('\n');
return input;
}
```