Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,21 @@
spiralArray = function (edge) {
var arr = Array(edge),
x = 0, y = edge,
total = edge * edge--,
dx = 1, dy = 0,
i = 0, j = 0;
while (y) arr[--y] = [];
while (i < total) {
arr[y][x] = i++;
x += dx; y += dy;
if (++j == edge) {
if (dy < 0) {x++; y++; edge -= 2}
j = dx; dx = -dy; dy = j; j = 0;
}
}
return arr;
}
// T E S T:
arr = spiralArray(edge = 5);
for (y= 0; y < edge; y++) console.log(arr[y].join(" "));

View file

@ -0,0 +1,67 @@
(function (n) {
// Spiral: the first row plus a smaller spiral rotated 90 degrees clockwise
function spiral(lngRows, lngCols, nStart) {
return lngRows ? [range(nStart, (nStart + lngCols) - 1)].concat(
transpose(
spiral(lngCols, lngRows - 1, nStart + lngCols)
).map(reverse)
) : [
[]
];
}
// rows and columns transposed (for 90 degree rotation)
function transpose(lst) {
return lst.length > 1 ? lst[0].map(function (_, col) {
return lst.map(function (row) {
return row[col];
});
}) : lst;
}
// elements in reverse order (for 90 degree rotation)
function reverse(lst) {
return lst.length > 1 ? lst.reduceRight(function (acc, x) {
return acc.concat(x);
}, []) : lst;
}
// [m..n]
function range(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
});
}
// TESTING
var lstSpiral = spiral(n, n, 0);
// OUTPUT FORMATTING - JSON and wikiTable
function wikiTable(lstRows, blnHeaderRow, strStyle) {
return '{| class="wikitable" ' + (
strStyle ? 'style="' + strStyle + '"' : ''
) + lstRows.map(function (lstRow, iRow) {
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) {
return typeof v === 'undefined' ? ' ' : v;
}).join(' ' + strDelim + strDelim + ' ');
}).join('') + '\n|}';
}
return [
wikiTable(
lstSpiral,
false,
'text-align:center;width:12em;height:12em;table-layout:fixed;'
),
JSON.stringify(lstSpiral)
].join('\n\n');
})(5);

View file

@ -0,0 +1 @@
[[0,1,2,3,4],[15,16,17,18,5],[14,23,24,19,6],[13,22,21,20,7],[12,11,10,9,8]]

View file

@ -0,0 +1,88 @@
(() => {
"use strict";
// ------------------ SPIRAL MATRIX ------------------
// spiral :: Int -> [[Int]]
const spiral = n => {
const go = (rows, cols, start) =>
Boolean(rows) ? [
enumFromTo(start)(start + pred(cols)),
...transpose(
go(
cols,
pred(rows),
start + cols
)
).map(reverse)
] : [
[]
];
return go(n, n, 0);
};
// ---------------------- TEST -----------------------
// main :: () -> String
const main = () => {
const
n = 5,
cellWidth = 1 + `${pred(n ** 2)}`.length;
return unlines(
spiral(n).map(
row => (
row.map(x => `${x}`
.padStart(cellWidth, " "))
)
.join("")
)
);
};
// --------------------- GENERIC ---------------------
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m =>
n => Array.from({
length: 1 + n - m
}, (_, i) => m + i);
// pred :: Enum a => a -> a
const pred = x => x - 1;
// reverse :: [a] -> [a]
const reverse = xs =>
"string" === typeof xs ? (
xs.split("").reverse()
.join("")
) : xs.slice(0).reverse();
// transpose :: [[a]] -> [[a]]
const transpose = rows =>
// The columns of the input transposed
// into new rows.
// Simpler version of transpose, assuming input
// rows of even length.
Boolean(rows.length) ? rows[0].map(
(_, i) => rows.flatMap(
v => v[i]
)
) : [];
// unlines :: [String] -> String
const unlines = xs =>
// A single string formed by the intercalation
// of a list of strings with the newline character.
xs.join("\n");
// MAIN ---
return main();
})();