September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,19 +1,18 @@
|
|||
var Roman = {
|
||||
Values: [['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]],
|
||||
|
||||
Values: [['CM', 900], ['CD', 400], ['XC', 90], ['XL', 40], ['IV', 4],
|
||||
['IX', 9], ['V', 5], ['X', 10], ['L', 50],
|
||||
['C', 100], ['M', 1000], ['I', 1], ['D', 500]],
|
||||
UnmappedStr : 'Q',
|
||||
parse: function(str) {
|
||||
var result = 0
|
||||
for (var i=0; i<Roman.Values.length; ++i) {
|
||||
var pair = Roman.Values[i]
|
||||
var key = pair[0]
|
||||
var value = pair[1]
|
||||
var regex = RegExp('^' + key)
|
||||
var regex = RegExp(key)
|
||||
while (str.match(regex)) {
|
||||
result += value
|
||||
str = str.replace(regex, '')
|
||||
str = str.replace(regex, Roman.UnmappedStr)
|
||||
}
|
||||
}
|
||||
return result
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
(() => {
|
||||
// romanValue :: String -> Int
|
||||
const romanValue = s =>
|
||||
s.length ? (() => {
|
||||
const parse = [].concat(
|
||||
...glyphs.map(g => 0 === s.indexOf(g) ? (
|
||||
[dctTrans[g], s.substr(g.length)]
|
||||
) : [])
|
||||
);
|
||||
return parse[0] + romanValue(parse[1]);
|
||||
})() : 0;
|
||||
|
||||
// dctTrans :: {romanKey: Integer}
|
||||
const dctTrans = {
|
||||
M: 1E3,
|
||||
CM: 900,
|
||||
D: 500,
|
||||
CD: 400,
|
||||
C: 100,
|
||||
XC: 90,
|
||||
L: 50,
|
||||
XL: 40,
|
||||
X: 10,
|
||||
IX: 9,
|
||||
V: 5,
|
||||
IV: 4,
|
||||
I: 1
|
||||
};
|
||||
|
||||
// glyphs :: [romanKey]
|
||||
const glyphs = Object.keys(dctTrans);
|
||||
|
||||
// TEST -------------------------------------------------------------------
|
||||
return ["MCMXC", "MDCLXVI", "MMVIII", "MMMM"].map(romanValue);
|
||||
})();
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1990,1666,2008,4000]
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
(() => {
|
||||
|
||||
// Folding from right to left,
|
||||
// lower leftward characters are subtracted,
|
||||
// others are added.
|
||||
|
||||
// fromRoman :: String -> Int
|
||||
const fromRoman = s =>
|
||||
snd(foldr(
|
||||
([r, n], l) => [l, l >= r ? n + l : n - l], [0, 0],
|
||||
map(charVal, stringChars(s))
|
||||
));
|
||||
|
||||
// charVal :: Char -> Maybe Int
|
||||
const charVal = k => {
|
||||
const v = {
|
||||
I: 1,
|
||||
V: 5,
|
||||
X: 10,
|
||||
L: 50,
|
||||
C: 100,
|
||||
D: 500,
|
||||
M: 1000
|
||||
}[k];
|
||||
return v !== undefined ? v : 0;
|
||||
};
|
||||
|
||||
// GENERIC FUNCTIONS ------------------------------------------------------
|
||||
|
||||
// foldr (a -> b -> b) -> b -> [a] -> b
|
||||
const foldr = (f, a, xs) => xs.reduceRight(f, a);
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// snd :: (a, b) -> b
|
||||
const snd = tpl => Array.isArray(tpl) ? tpl[1] : undefined;
|
||||
|
||||
// stringChars :: String -> [Char]
|
||||
const stringChars = s => s.split('');
|
||||
|
||||
// show :: a -> String
|
||||
const show = (...x) =>
|
||||
JSON.stringify.apply(
|
||||
null, x.length > 1 ? [x[1], null, x[0]] : x
|
||||
);
|
||||
|
||||
// TEST -------------------------------------------------------------------
|
||||
return show(
|
||||
map(fromRoman, ["MDCLXVI", "MCMXC", "MMVIII", "MMXVI", "MMXVII"])
|
||||
);
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue