Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,26 @@
|
|||
var Roman = {
|
||||
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)
|
||||
while (str.match(regex)) {
|
||||
result += value
|
||||
str = str.replace(regex, Roman.UnmappedStr)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
var test_data = ['MCMXC', 'MDCLXVI', 'MMVIII']
|
||||
for (var i=0; i<test_data.length; ++i) {
|
||||
var test_datum = test_data[i]
|
||||
print(test_datum + ": " + Roman.parse(test_datum))
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
(function (lstTest) {
|
||||
|
||||
var mapping = [["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]];
|
||||
|
||||
// s -> n
|
||||
function romanValue(s) {
|
||||
// recursion over list of characters
|
||||
// [c] -> n
|
||||
function toArabic(lst) {
|
||||
return lst.length ? function (xs) {
|
||||
var lstParse = chain(mapping, function (lstPair) {
|
||||
return isPrefixOf(
|
||||
lstPair[0], xs
|
||||
) ? [lstPair[1], drop(lstPair[0].length, xs)] : []
|
||||
});
|
||||
return lstParse[0] + toArabic(lstParse[1]);
|
||||
}(lst) : 0
|
||||
}
|
||||
return toArabic(s.split(''));
|
||||
}
|
||||
|
||||
// Monadic bind (chain) for lists
|
||||
function chain(xs, f) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
}
|
||||
|
||||
// [a] -> [a] -> Bool
|
||||
function isPrefixOf(lstFirst, lstSecond) {
|
||||
return lstFirst.length ? (
|
||||
lstSecond.length ?
|
||||
lstFirst[0] === lstSecond[0] && isPrefixOf(
|
||||
lstFirst.slice(1), lstSecond.slice(1)
|
||||
) : false
|
||||
) : true;
|
||||
}
|
||||
|
||||
// Int -> [a] -> [a]
|
||||
function drop(n, lst) {
|
||||
return n <= 0 ? lst : (
|
||||
lst.length ? drop(n - 1, lst.slice(1)) : []
|
||||
);
|
||||
}
|
||||
|
||||
return lstTest.map(romanValue);
|
||||
|
||||
})(['MCMXC', 'MDCLXVI', 'MMVIII']);
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1990, 1666, 2008]
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
(function (lstTest) {
|
||||
|
||||
function romanValue(s) {
|
||||
return s.length ? function () {
|
||||
var parse = [].concat.apply([], glyphs.map(function (g) {
|
||||
return 0 === s.indexOf(g) ? [trans[g], s.substr(g.length)] : [];
|
||||
}));
|
||||
return parse[0] + romanValue(parse[1]);
|
||||
}() : 0;
|
||||
}
|
||||
|
||||
var trans = {
|
||||
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 = Object.keys(trans);
|
||||
|
||||
return lstTest.map(romanValue);
|
||||
|
||||
})(["MCMXC", "MDCLXVI", "MMVIII", "MMMM"]);
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1990, 1666, 2008]
|
||||
|
|
@ -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,56 @@
|
|||
(() => {
|
||||
|
||||
// -------------- ROMAN NUMERALS DECODED ---------------
|
||||
|
||||
// Folding from right to left,
|
||||
// lower leftward characters are subtracted,
|
||||
// others are added.
|
||||
|
||||
// fromRoman :: String -> Int
|
||||
const fromRoman = s =>
|
||||
foldr(l => ([r, n]) => [
|
||||
l,
|
||||
l >= r ? (
|
||||
n + l
|
||||
) : n - l
|
||||
])([0, 0])(
|
||||
[...s].map(charVal)
|
||||
)[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');
|
||||
|
||||
|
||||
// ----------------- 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();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue