Roman numerals/Decode: Difference between revisions

Updated D versions
(Updated D versions)
Line 371:
<lang d>import std.regex, std.algorithm;
 
int toArabic(in string s) /*pure nothrow*/ {
static constimmutable weights = [1000, 900, 500, 400, 100, 90, 50,
90, 50, 40, 10, 9, 5, 4, 1];
static /*const*/immutable symbols = ["M","CM","D","CD","C","XC",
"L","XL","X","IX","V","IV","I"];
 
int arabic;
foreach (m; s.match(s, regex(r"CM|CD|XC|XL|IX|IV|[MDCLXVI]", "g")))
arabic += weights[symbols.countUntil(m.hit)];
 
return arabic;
}
Line 391:
<lang d>import std.regex, std.algorithm;
 
immutable int[string] w2s;
nothrow static this() {
w2s = ["IX": 9, "C": 100, "D": 500, "CM": 900, "I": 1,
"XC": 90, "M": 1000, "L": 50, "CD": 400, "XL": 40,
"V": 5, "X": 10, "IV": 4];
}
 
int toArabic(in string s) /*pure nothrow*/ {
auto ms = match(s, regex(r"CM|CD|XC|XL|IX|IV|[MDCLXVI]", "g"));
return reduce!q{ ((a, + b }(map!((m){ return=> a + w2s[m.hit]; })(0, ms));
}
 
Anonymous user