object RomanToInteger {
private val romanMap: Map[Char, Int] = Map(
'I' -> 1,
'V' -> 5,
'X' -> 10,
'L' -> 50,
'C' -> 100,
'D' -> 500,
'M' -> 1000
)
def romanToInt(s: String): Int = {
var total = 0
var prevValue = 0
for (ch <- s.reverse) {
val currentValue = romanMap(ch)
if (currentValue < prevValue) {
total -= currentValue
} else {
total += currentValue
}
prevValue = currentValue
}
total
}
def main(args: Array[String]): Unit = {
val roman = "XCVII"
val result = romanToInt(roman)
println(s"The integer value of $roman is $result")
}
}
/*
XCVII =
XC+V+I+I =
90+5+1+1 =
97
*/
/*
run:
The integer value of XCVII is 97
*/