Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/Roman-numerals-Decode/D/roman-numerals-decode-1.d
Normal file
19
Task/Roman-numerals-Decode/D/roman-numerals-decode-1.d
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import std.regex, std.algorithm;
|
||||
|
||||
int toArabic(in string s) /*pure nothrow*/ {
|
||||
static immutable weights = [1000, 900, 500, 400, 100,
|
||||
90, 50, 40, 10, 9, 5, 4, 1];
|
||||
static immutable symbols = ["M","CM","D","CD","C","XC",
|
||||
"L","XL","X","IX","V","IV","I"];
|
||||
|
||||
int arabic;
|
||||
foreach (m; s.matchAll("CM|CD|XC|XL|IX|IV|[MDCLXVI]".regex))
|
||||
arabic += weights[symbols.countUntil(m.hit)];
|
||||
return arabic;
|
||||
}
|
||||
|
||||
void main() {
|
||||
assert("MCMXC".toArabic == 1990);
|
||||
assert("MMVIII".toArabic == 2008);
|
||||
assert("MDCLXVI".toArabic == 1666);
|
||||
}
|
||||
22
Task/Roman-numerals-Decode/D/roman-numerals-decode-2.d
Normal file
22
Task/Roman-numerals-Decode/D/roman-numerals-decode-2.d
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import std.regex, std.algorithm;
|
||||
|
||||
immutable uint[string] w2s;
|
||||
|
||||
pure 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];
|
||||
}
|
||||
|
||||
uint toArabic(in string s) /*pure nothrow*/ @safe /*@nogc*/ {
|
||||
return s
|
||||
.matchAll("CM|CD|XC|XL|IX|IV|[MDCLXVI]".regex)
|
||||
.map!(m => w2s[m.hit])
|
||||
.sum;
|
||||
}
|
||||
|
||||
void main() {
|
||||
assert("MCMXC".toArabic == 1990);
|
||||
assert("MMVIII".toArabic == 2008);
|
||||
assert("MDCLXVI".toArabic == 1666);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue