2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,50 +1,49 @@
function roman(strIntegers) {
'use strict';
// DICTIONARY OF GLYPH:VALUE MAPPINGS
var dctGlyphs = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
X: 10,
IX: 9,
V: 5,
IV: 4,
I: 1
};
// LIST OF INTEGER STRINGS, WITH ANY SEPARATOR
var strNums = typeof strIntegers === 'string' ? strIntegers : strIntegers.toString(),
lstParts = strNums.split(/\d+/),
strSeparator = lstParts.length > 1 ? lstParts[1] : '',
lstDecimal = strSeparator ? strIntegers.split(strSeparator) : [strNums];
(function () {
'use strict';
// REWRITE OF DECIMAL INTEGER AS ROMAN
function rewrite(strN) {
var n = Number(strN);
// If the Roman is a string, pass any delimiters through
/* Starting with the highest-valued glyph:
take as many bites as we can with it
(decrementing residual value with each bite,
and appending a corresponding glyph copy to the string)
before moving down to the next most expensive glyph */
// (Int | String) -> String
function romanTranscription(a) {
if (typeof a === 'string') {
var ps = a.split(/\d+/),
dlm = ps.length > 1 ? ps[1] : undefined;
// return Object.keys(dctGlyphs).reduce(
// OR:
return 'M CM D CD C XC L XL X IX V IV I'.split(' ').reduce(
function (s, k) {
var v = dctGlyphs[k];
return n >= v ? (n -= v, s + k) : s;
}, ''
)
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];
// ALL REWRITTEN, WITH SEPARATOR RESTORED
return lstDecimal.map(rewrite).join(strSeparator);
}
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

@ -1,5 +1 @@
roman(1999);
// --> "MCMXCIX"
[1990, 2008, "14.09.2015", 2000, 1666].map(roman);
// --> ["MCMXC", "MCMCVI", "XIV.IX.MCMCXV", "MCMC", "MDCLXVI"]
["MMXVI", "MCMXC", "MMVIII", "XIV.IX.MMXV", "MM", "MDCLXVI"]

View file

@ -0,0 +1,55 @@
(() => {
'use strict';
// roman :: Int -> String
const roman = n => [
[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((a, lstPair) => {
const 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
// If the input is a decimal string, pass any delimiters through
// romanTranscription :: (Int | String) -> String
const romanTranscription = a => {
if (typeof a === 'string') {
const ps = a.split(/\d+/),
dlm = ps.length > 1 ? ps[1] : undefined;
return (dlm ? a.split(dlm)
.map(Number) : [a])
.map(roman)
.join(dlm);
} else return roman(a);
}
// TEST
return [2016, 1990, 2008, "14.09.2015", 2000, 1666]
.map(romanTranscription);
})();

View file

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