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,71 @@
// Pascal's triangle object
function pascalTriangle (rows) {
// Number of rows the triangle contains
this.rows = rows;
// The 2D array holding the rows of the triangle
this.triangle = new Array();
for (var r = 0; r < rows; r++) {
this.triangle[r] = new Array();
for (var i = 0; i <= r; i++) {
if (i == 0 || i == r)
this.triangle[r][i] = 1;
else
this.triangle[r][i] = this.triangle[r-1][i-1]+this.triangle[r-1][i];
}
}
// Method to print the triangle
this.print = function(base) {
if (!base)
base = 10;
// Private method to calculate digits in number
var digits = function(n,b) {
var d = 0;
while (n >= 1) {
d++;
n /= b;
}
return d;
}
// Calculate max spaces needed
var spacing = digits(this.triangle[this.rows-1][Math.round(this.rows/2)],base);
// Private method to add spacing between numbers
var insertSpaces = function(s) {
var buf = "";
while (s > 0) {
s--;
buf += " ";
}
return buf;
}
// Print the triangle line by line
for (var r = 0; r < this.triangle.length; r++) {
var l = "";
for (var s = 0; s < Math.round(this.rows-1-r); s++) {
l += insertSpaces(spacing);
}
for (var i = 0; i < this.triangle[r].length; i++) {
if (i != 0)
l += insertSpaces(spacing-Math.ceil(digits(this.triangle[r][i],base)/2));
l += this.triangle[r][i].toString(base);
if (i < this.triangle[r].length-1)
l += insertSpaces(spacing-Math.floor(digits(this.triangle[r][i],base)/2));
}
print(l);
}
}
}
// Display 4 row triangle in base 10
var tri = new pascalTriangle(4);
tri.print();
// Display 8 row triangle in base 16
tri = new pascalTriangle(8);
tri.print(16);

View file

@ -0,0 +1,102 @@
(function (n) {
'use strict';
// PASCAL TRIANGLE --------------------------------------------------------
// pascal :: Int -> [[Int]]
function pascal(n) {
return foldl(function (a) {
var xs = a.slice(-1)[0]; // Previous row
return append(a, [zipWith(
function (a, b) {
return a + b;
},
append([0], xs),
append(xs, [0])
)]);
}, [
[1] // Initial seed row
], enumFromTo(1, n - 1));
};
// GENERIC FUNCTIONS ------------------------------------------------------
// (++) :: [a] -> [a] -> [a]
function append(xs, ys) {
return xs.concat(ys);
};
// enumFromTo :: Int -> Int -> [Int]
function enumFromTo(m, n) {
return Array.from({
length: Math.floor(n - m) + 1
}, function (_, i) {
return m + i;
});
};
// foldl :: (b -> a -> b) -> b -> [a] -> b
function foldl(f, a, xs) {
return xs.reduce(f, a);
};
// foldr (a -> b -> b) -> b -> [a] -> b
function foldr(f, a, xs) {
return xs.reduceRight(f, a);
};
// map :: (a -> b) -> [a] -> [b]
function map(f, xs) {
return xs.map(f);
};
// min :: Ord a => a -> a -> a
function min(a, b) {
return b < a ? b : a;
};
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
function zipWith(f, xs, ys) {
return Array.from({
length: min(xs.length, ys.length)
}, function (_, i) {
return f(xs[i], ys[i]);
});
};
// TEST and FORMAT --------------------------------------------------------
var lstTriangle = pascal(n);
// [[a]] -> bool -> s -> s
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|}';
}
var lstLastLine = lstTriangle.slice(-1)[0],
lngBase = lstLastLine.length * 2 - 1,
nWidth = lstLastLine.reduce(function (a, x) {
var d = x.toString()
.length;
return d > a ? d : a;
}, 1) * lngBase;
return [wikiTable(lstTriangle.map(function (lst) {
return lst.join(';;')
.split(';');
})
.map(function (line, i) {
var lstPad = Array((lngBase - line.length) / 2);
return lstPad.concat(line)
.concat(lstPad);
}), false, 'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
'em;table-layout:fixed;'), JSON.stringify(lstTriangle)].join('\n\n');
})(7);

View file

@ -0,0 +1 @@
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1],[1,6,15,20,15,6,1]]

View file

@ -0,0 +1,109 @@
(() => {
"use strict";
// ---------------- PASCAL'S TRIANGLE ----------------
// pascal :: Generator [[Int]]
const pascal = () =>
iterate(
xs => zipWith(
a => b => a + b
)(
[0, ...xs]
)(
[...xs, 0]
)
)([1]);
// ---------------------- TEST -----------------------
// main :: IO ()
const main = () =>
showPascal(
take(10)(
pascal()
)
);
// showPascal :: [[Int]] -> String
const showPascal = xs => {
const w = last(xs).join(" ").length;
return xs.map(
ys => center(w)(" ")(ys.join(" "))
)
.join("\n");
};
// ---------------- GENERIC FUNCTIONS ----------------
// center :: Int -> Char -> String -> String
const center = n =>
// Size of space -> filler Char ->
// String -> Centered String
c => s => {
const gap = n - s.length;
return 0 < gap ? (() => {
const
margin = c.repeat(Math.floor(gap / 2)),
dust = c.repeat(gap % 2);
return `${margin}${s}${margin}${dust}`;
})() : s;
};
// iterate :: (a -> a) -> a -> Gen [a]
const iterate = f =>
// An infinite list of repeated
// applications of f to x.
function* (x) {
let v = x;
while (true) {
yield v;
v = f(v);
}
};
// last :: [a] -> a
const last = xs =>
0 < xs.length ? xs.slice(-1)[0] : undefined;
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take = n =>
// The first n elements of a list,
// string of characters, or stream.
xs => "GeneratorFunction" !== xs
.constructor.constructor.name ? (
xs.slice(0, n)
) : Array.from({
length: n
}, () => {
const x = xs.next();
return x.done ? [] : [x.value];
}).flat();
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = f =>
// A list constructed by zipping with a
// custom function, rather than with the
// default tuple constructor.
xs => ys => xs.map(
(x, i) => f(x)(ys[i])
).slice(
0, Math.min(xs.length, ys.length)
);
// MAIN ---
return main();
})();

View file

@ -0,0 +1,12 @@
const aux = n => {
if(n <= 1) return [1]
const prevLayer = aux(n - 1)
const shifted = [0, ...prevLayer]
return shifted.map((x, i) => (prevLayer[i] || 0) + x)
}
const pascal = n => {
for(let i = 1; i <= n; i++) {
console.log(aux(i).join(' '))
}
}
pascal(8)

View file

@ -0,0 +1,16 @@
const aux = (() => {
const layers = [[1], [1]]
return n => {
if(layers[n]) return layers[n]
const prevLayer = aux(n - 1)
const shifted = [0, ...prevLayer]
layers[n] = shifted.map((x, i) => (prevLayer[i] || 0) + x)
return layers[n]
}
})()
const pascal = n => {
for(let i = 1; i <= n; i++) {
console.log(aux(i).join(' '))
}
}
pascal(8)