5.2 KiB
5.2 KiB
title, id, localeTitle, challengeType
title | id | localeTitle | challengeType |
---|---|---|---|
Align columns | 594810f028c0303b75339ad0 | 594810f028c0303b75339ad0 | 5 |
Description
Dado un archivo de texto de muchas líneas, donde los campos dentro de una línea están delineados por un solo carácter de $
, escriba un programa que alinee cada columna de campos asegurándose de que las palabras en cada columna estén separadas por al menos un espacio. Además, permita que cada palabra en una columna se justifique a la izquierda, a la derecha o al centro dentro de su columna.
Usa el siguiente texto para probar tus programas:
Dado $ a $ texto $ archivo $ de $ muchas $ líneas donde $ campos $ dentro de $ a $ línea $ son $ delineados $ por $ a $ simple $ 'dólar' $ carácter escribe $ a $ programa ese $ alinea $ cada $ columna $ de $ campos por $ asegurando $ que $ palabras $ en $ cada $ columna $ están $ separados $ por $ en $ al menos $ un $ espacio. Además, $ permita $ por $ cada $ palabra $ en $ a $ columna $ a $ sea $ o bien $ dejó $ justificado, $ derecho $ justificó o $ centro $ justificado dentro de $ su $ columna.
Tenga en cuenta que:
Las líneas de texto de entrada de ejemplo pueden, o no, tener caracteres de dólar al final. Todas las columnas deben compartir la misma alineación. Los caracteres de espacio consecutivo producidos adyacentes al final de las líneas son insignificantes para los propósitos de la tarea. El texto de salida se verá en una fuente mono-espaciada en un editor de texto plano o terminal básico. El espacio mínimo entre columnas debe calcularse a partir del texto y no estar codificado. No es un requisito agregar caracteres de separación entre o alrededor de las columnas.Instructions
Tests
tests:
- text: <code>formatText</code> es una función.
testString: 'assert(typeof formatText === "function", "<code>formatText</code> is a function.");'
- text: ' <code>formatText</code> con la entrada anterior y la justificación "correcta" debe producir lo siguiente:'
testString: 'assert.strictEqual(formatText(testInput, "right"), rightAligned, "<code>formatText</code> with the above input and "right" justification should produce the following: ");'
- text: ' <code>formatText</code> con la entrada anterior y la justificación "izquierda" debe producir lo siguiente:'
testString: 'assert.strictEqual(formatText(testInput, "left"), leftAligned, "<code>formatText</code> with the above input and "left" justification should produce the following: ");'
- text: ' <code>formatText</code> con la entrada anterior y la justificación "central" debe producir lo siguiente:'
testString: 'assert.strictEqual(formatText(testInput, "center"), centerAligned, "<code>formatText</code> with the above input and "center" justification should produce the following: ");'
Challenge Seed
const testArr = [
'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.'
];
function formatText (input, justification) {
// Good luck!
}
After Test
console.info('after the test');
Solution
const testArr = [
'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.'
];
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;
}