Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,31 @@
var justification="center",
input=["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."],
x,y,cols,max,cols=0,diff,left,right
String.prototype.repeat=function(n){return new Array(1 + parseInt(n)).join(this);}
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");
document.write(input);

View file

@ -0,0 +1,48 @@
//break up each string by '$'. The assumption is that the user wants the trailing $.
var data = [
"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."
].map(function (str) { return str.split('$'); })
//boilerplate: get longest array or string in array
var getLongest = function (arr) {
return arr.reduce(function (acc, item) { return acc.length > item.length ? acc : item; }, 0);
};
//boilerplate: this function would normally be in a library like underscore, lodash, or ramda
var zip = function (items, toInsert) {
toInsert = (toInsert === undefined) ? null : toInsert;
var longestItem = getLongest(items);
return longestItem.map(function (_unused, index) {
return items.map(function (item) {
return item[index] === undefined ? toInsert : item[index];
});
});
};
//here's the part that's not boilerplate
var makeColumns = function (formatting, data) {
var zipData = zip(data, '');
var makeSpaces = function (num) { return new Array(num + 1).join(' '); };
var formattedCols = zipData.map(function (column) {
var maxLen = getLongest(column).length;//find the maximum word length
if (formatting === 'left') {
return column.map(function (word) { return word + makeSpaces(maxLen - word.length); });
} else if (formatting === 'right') {
return column.map(function (word) { return makeSpaces(maxLen - word.length) + word; });
} else {
return column.map(function (word) {
var spaces = maxLen - word.length,
first = ~~(spaces / 2),
last = spaces - first;
return makeSpaces(first) + word + makeSpaces(last);
});
}
});
return zip(formattedCols).map(function (row) { return row.join(' '); }).join('\n');
};

View file

@ -0,0 +1,133 @@
(function (strText) {
'use strict';
// [[a]] -> [[a]]
function transpose(lst) {
return lst[0].map(function (_, iCol) {
return lst.map(function (row) {
return row[iCol];
})
});
}
// (a -> b -> c) -> [a] -> [b] -> [c]
function zipWith(f, xs, ys) {
return xs.length === ys.length ? (
xs.map(function (x, i) {
return f(x, ys[i]);
})
) : undefined;
}
// (a -> a -> Ordering) -> [a] -> a
function maximumBy(f, xs) {
return xs.reduce(function (a, x) {
return a === undefined ? x : (
f(x) > f(a) ? x : a
);
}, undefined)
}
// [String] -> String
function widest(lst) {
return maximumBy(length, lst)
.length;
}
// [[a]] -> [[a]]
function fullRow(lst, n) {
return lst.concat(Array.apply(null, Array(n - lst.length))
.map(function () {
return ''
}));
}
// String -> Int -> String
function nreps(s, n) {
var o = '';
if (n < 1) return o;
while (n > 1) {
if (n & 1) o += s;
n >>= 1;
s += s;
}
return o + s;
}
// [String] -> String
function unwords(xs) {
return xs.join(' ');
}
// [String] -> String
function unlines(xs) {
return xs.join('\n');
}
// [a] -> Int
function length(xs) {
return xs.length;
}
// -- Int -> [String] -> [[String]]
function padWords(n, lstWords, eAlign) {
return lstWords.map(function (w) {
var lngPad = n - w.length;
return (
(eAlign === eCenter) ? (function () {
var lngHalf = Math.floor(lngPad / 2);
return [
nreps(' ', lngHalf), w,
nreps(' ', lngPad - lngHalf)
];
})() : (eAlign === eLeft) ?
['', w, nreps(' ', lngPad)] :
[nreps(' ', lngPad), w, '']
)
.join('');
});
}
// MAIN
var eLeft = -1,
eCenter = 0,
eRight = 1;
var lstRows = strText.split('\n')
.map(function (x) {
return x.split('$');
}),
lngCols = widest(lstRows),
lstCols = transpose(lstRows.map(function (r) {
return fullRow(r, lngCols)
})),
lstColWidths = lstCols.map(widest);
// THREE PARAGRAPHS, WITH VARIOUS WORD COLUMN ALIGNMENTS:
return [eLeft, eRight, eCenter]
.map(function (eAlign) {
var fPad = function (n, lstWords) {
return padWords(n, lstWords, eAlign);
};
return transpose(
zipWith(fPad, lstColWidths, lstCols)
)
.map(unwords);
})
.map(unlines)
.join('\n\n');
})(
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n\
are$delineated$by$a$single$'dollar'$character,$write$a$program\n\
that$aligns$each$column$of$fields$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,$or$center$justified$within$its$column."
);