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,15 @@
function median(ary) {
if (ary.length == 0)
return null;
ary.sort(function (a,b){return a - b})
var mid = Math.floor(ary.length / 2);
if ((ary.length % 2) == 1) // length is odd
return ary[mid];
else
return (ary[mid - 1] + ary[mid]) / 2;
}
median([]); // null
median([5,3,4]); // 4
median([5,4,2,3]); // 3.5
median([3,4,1,-8.4,7.2,4,1,1.2]); // 2.1

View file

@ -0,0 +1,52 @@
(() => {
'use strict';
// median :: [Num] -> Num
function median(xs) {
// nth :: [Num] -> Int -> Maybe Num
let nth = (xxs, n) => {
if (xxs.length > 0) {
let [x, xs] = uncons(xxs),
[ys, zs] = partition(y => y < x, xs),
k = ys.length;
return k === n ? x : (
k > n ? nth(ys, n) : nth(zs, n - k - 1)
);
} else return undefined;
},
n = xs.length;
return even(n) ? (
(nth(xs, div(n, 2)) + nth(xs, div(n, 2) - 1)) / 2
) : nth(xs, div(n, 2));
}
// GENERIC
// partition :: (a -> Bool) -> [a] -> ([a], [a])
let partition = (p, xs) =>
xs.reduce((a, x) =>
p(x) ? [a[0].concat(x), a[1]] : [a[0], a[1].concat(x)], [
[],
[]
]),
// uncons :: [a] -> Maybe (a, [a])
uncons = xs => xs.length ? [xs[0], xs.slice(1)] : undefined,
// even :: Integral a => a -> Bool
even = n => n % 2 === 0,
// div :: Num -> Num -> Int
div = (x, y) => Math.floor(x / y);
return [
[],
[5, 3, 4],
[5, 4, 2, 3],
[3, 4, 1, -8.4, 7.2, 4, 1, 1.2]
].map(median);
})();

View file

@ -0,0 +1,6 @@
[
null,
4,
3.5,
2.1
]