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,18 @@
var roman = {
map: [
1000, 'M', 900, 'CM', 500, 'D', 400, 'CD', 100, 'C', 90, 'XC',
50, 'L', 40, 'XL', 10, 'X', 9, 'IX', 5, 'V', 4, 'IV', 1, 'I',
],
int_to_roman: function(n) {
var value = '';
for (var idx = 0; n > 0 && idx < this.map.length; idx += 2) {
while (n >= this.map[idx]) {
value += this.map[idx + 1];
n -= this.map[idx];
}
}
return value;
}
}
roman.int_to_roman(1999); // "MCMXCIX"

View file

@ -0,0 +1,49 @@
(function () {
'use strict';
// If the Roman is a string, pass any delimiters through
// (Int | String) -> String
function romanTranscription(a) {
if (typeof a === 'string') {
var ps = a.split(/\d+/),
dlm = ps.length > 1 ? ps[1] : undefined;
return (dlm ? a.split(dlm)
.map(function (x) {
return Number(x);
}) : [a])
.map(roman)
.join(dlm);
} else return roman(a);
}
// roman :: Int -> String
function roman(n) {
return [[1000, "M"], [900, "CM"], [500, "D"], [400, "CD"], [100,
"C"], [90, "XC"], [50, "L"], [40, "XL"], [10, "X"], [9,
"IX"], [5, "V"], [4, "IV"], [1, "I"]]
.reduce(function (a, lstPair) {
var m = a.remainder,
v = lstPair[0];
return (v > m ? a : {
remainder: m % v,
roman: a.roman + Array(
Math.floor(m / v) + 1
)
.join(lstPair[1])
});
}, {
remainder: n,
roman: ''
}).roman;
}
// TEST
return [2016, 1990, 2008, "14.09.2015", 2000, 1666].map(
romanTranscription);
})();

View file

@ -0,0 +1 @@
["MMXVI", "MCMXC", "MMVIII", "XIV.IX.MMXV", "MM", "MDCLXVI"]

View file

@ -0,0 +1,78 @@
(() => {
"use strict";
// -------------- ROMAN INTEGER STRINGS --------------
// roman :: Int -> String
const roman = n =>
mapAccumL(residue =>
([k, v]) => second(
q => 0 < q ? (
k.repeat(q)
) : ""
)(remQuot(residue)(v))
)(n)(
zip([
"M", "CM", "D", "CD", "C", "XC",
"L", "XL", "X", "IX", "V", "IV", "I"
])([
1000, 900, 500, 400, 100, 90,
50, 40, 10, 9, 5, 4, 1
])
)[1]
.join("");
// ---------------------- TEST -----------------------
// main :: IO ()
const main = () => (
[2016, 1990, 2008, 2000, 2020, 1666].map(roman)
).join("\n");
// ---------------- GENERIC FUNCTIONS ----------------
// mapAccumL :: (acc -> x -> (acc, y)) -> acc ->
// [x] -> (acc, [y])
const mapAccumL = f =>
// A tuple of an accumulation and a list
// obtained by a combined map and fold,
// with accumulation from left to right.
acc => xs => [...xs].reduce(
(a, x) => {
const tpl = f(a[0])(x);
return [
tpl[0],
a[1].concat(tpl[1])
];
},
[acc, []]
);
// remQuot :: Int -> Int -> (Int, Int)
const remQuot = m =>
n => [m % n, Math.trunc(m / n)];
// second :: (a -> b) -> ((c, a) -> (c, b))
const second = f =>
// A function over a simple value lifted
// to a function over a tuple.
// f (a, b) -> (a, f(b))
xy => [xy[0], f(xy[1])];
// zip :: [a] -> [b] -> [(a, b)]
const zip = xs =>
// The paired members of xs and ys, up to
// the length of the shorter of the two lists.
ys => Array.from({
length: Math.min(xs.length, ys.length)
}, (_, i) => [xs[i], ys[i]]);
// MAIN --
return main();
})();

View file

@ -0,0 +1,18 @@
function toRoman(num) {
return 'I'
.repeat(num)
.replace(/IIIII/g, 'V')
.replace(/VV/g, 'X')
.replace(/XXXXX/g, 'L')
.replace(/LL/g, 'C')
.replace(/CCCCC/g, 'D')
.replace(/DD/g, 'M')
.replace(/VIIII/g, 'IX')
.replace(/LXXXX/g, 'XC')
.replace(/XXXX/g, 'XL')
.replace(/DCCCC/g, 'CM')
.replace(/CCCC/g, 'CD')
.replace(/IIII/g, 'IV');
}
console.log(toRoman(1666));

View file

@ -0,0 +1 @@
MDCLXVI