object RomanToInteger {
private val romanMap: Map<Char, Int> = mapOf(
'I' to 1,
'V' to 5,
'X' to 10,
'L' to 50,
'C' to 100,
'D' to 500,
'M' to 1000
)
fun romanToInt(s: String): Int {
var total = 0
var prevValue = 0
for (ch in s.reversed()) {
val currentValue = romanMap[ch] ?: 0
if (currentValue < prevValue) {
total -= currentValue
} else {
total += currentValue
}
prevValue = currentValue
}
return total
}
@JvmStatic
fun main(args: Array<String>) {
val roman = "XCVII"
val result = romanToInt(roman)
println("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
*/