Data update
This commit is contained in:
parent
29a5eea0d4
commit
5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions
|
|
@ -1,6 +1,7 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// -------------- ROMAN NUMERALS DECODED ---------------
|
||||
// ------------- ROMAN NUMERALS DECODED --------------
|
||||
|
||||
// Folding from right to left,
|
||||
// lower leftward characters are subtracted,
|
||||
|
|
@ -8,49 +9,34 @@
|
|||
|
||||
// fromRoman :: String -> Int
|
||||
const fromRoman = s =>
|
||||
foldr(l => ([r, n]) => [
|
||||
l,
|
||||
l >= r ? (
|
||||
n + l
|
||||
) : n - l
|
||||
])([0, 0])(
|
||||
[...s].map(charVal)
|
||||
[...s]
|
||||
.map(glyphValue)
|
||||
.reduceRight(
|
||||
([r, n], l) => [
|
||||
l,
|
||||
l >= r
|
||||
? n + l
|
||||
: n - l
|
||||
],
|
||||
[0, 0]
|
||||
)[1];
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
||||
// ----------------------- TEST ------------------------
|
||||
const main = () => [
|
||||
'MDCLXVI', 'MCMXC', 'MMVIII', 'MMXVI', 'MMXVII'
|
||||
]
|
||||
.map(fromRoman)
|
||||
.join('\n');
|
||||
// glyphValue :: Char -> Maybe Int
|
||||
const glyphValue = k => ({
|
||||
I: 1,
|
||||
V: 5,
|
||||
X: 10,
|
||||
L: 50,
|
||||
C: 100,
|
||||
D: 500,
|
||||
M: 1000
|
||||
}) [k] || 0;
|
||||
|
||||
|
||||
// ----------------- GENERIC FUNCTIONS -----------------
|
||||
|
||||
// foldr :: (a -> b -> b) -> b -> [a] -> b
|
||||
const foldr = f =>
|
||||
// Note that that the Haskell signature of foldr
|
||||
// differs from that of foldl - the positions of
|
||||
// accumulator and current value are reversed.
|
||||
a => xs => [...xs].reduceRight(
|
||||
(a, x) => f(x)(a),
|
||||
a
|
||||
);
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
// ---------------------- TEST -----------------------
|
||||
return [
|
||||
"MDCLXVI", "MCMXC", "MMVIII", "MMXVI", "MMXVII"
|
||||
]
|
||||
.map(fromRoman)
|
||||
.join("\n");
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue