29 lines
560 B
Text
29 lines
560 B
Text
|
|
func roman2arabic(roman) {
|
|||
|
|
|
|||
|
|
var arabic = 0
|
|||
|
|
var last_digit = 1000
|
|||
|
|
|
|||
|
|
static m = Hash(
|
|||
|
|
I => 1,
|
|||
|
|
V => 5,
|
|||
|
|
X => 10,
|
|||
|
|
L => 50,
|
|||
|
|
C => 100,
|
|||
|
|
D => 500,
|
|||
|
|
M => 1000,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
roman.uc.chars.map{m{_} \\ 0}.each { |digit|
|
|||
|
|
if (last_digit < digit) {
|
|||
|
|
arabic -= (2 * last_digit)
|
|||
|
|
}
|
|||
|
|
arabic += (last_digit = digit)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return arabic
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
%w(MCMXC MMVIII MDCLXVI).each { |roman_digit|
|
|||
|
|
"%-10s == %d\n".printf(roman_digit, roman2arabic(roman_digit))
|
|||
|
|
}
|