Files
freeCodeCamp/curriculum/challenges/ukrainian/22-rosetta-code/rosetta-code-challenges/align-columns.md
camperbot 7a0d396180 chore(i18n,learn): processed translations (#53415)
Co-authored-by: Naomi Carrigan <nhcarrigan@gmail.com>
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2024-02-13 18:31:01 +01:00

190 lines
7.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: 594810f028c0303b75339ad0
title: Вирівнювання стовпців
challengeType: 1
forumTopicId: 302224
dashedName: align-columns
---
# --description--
Вам надано масив з багатьма рядками, де поля в межах рядків обмежені символом `$`. Напишіть програму, яка вирівнює кожен стовпець поля, переконавшись, що слова в кожному стовпці розділені принаймні одним пробілом. Крім того, дозвольте кожному слову в стовпці вирівнюватись за лівим краєм, за правим краєм або по центру.
# --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;
}
```