tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 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.match(regex(r"CM|CD|XC|XL|IX|IV|[MDCLXVI]", "g")))
|
||||
arabic += weights[symbols.countUntil(m.hit)];
|
||||
return arabic;
|
||||
}
|
||||
|
||||
void main() {
|
||||
assert(toArabic("MCMXC") == 1990);
|
||||
assert(toArabic("MMVIII") == 2008);
|
||||
assert(toArabic("MDCLXVI") == 1666);
|
||||
}
|
||||
20
Task/Roman-numerals-Decode/D/roman-numerals-decode-2.d
Normal file
20
Task/Roman-numerals-Decode/D/roman-numerals-decode-2.d
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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("CM|CD|XC|XL|IX|IV|[MDCLXVI]", "g"));
|
||||
return reduce!((a, m) => a + w2s[m.hit])(0, ms);
|
||||
}
|
||||
|
||||
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