import Foundation
struct RomanToInteger {
private static let romanMap: [Character: Int] = [
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
]
static func romanToInt(_ s: String) -> Int {
var total = 0
var prevValue = 0
for ch in s.reversed() {
let currentValue = romanMap[ch] ?? 0
if currentValue < prevValue {
total -= currentValue
} else {
total += currentValue
}
prevValue = currentValue
}
return total
}
}
// main
let roman = "XCVII"
let result = RomanToInteger.romanToInt(roman)
print("The integer value of \(roman) is \(result)")
/*
XCVII =
XC+V+I+I =
90+5+1+1 =
97
*/
/*
run:
"This is a"
"programming"
"example of"
"text"
"wrapping"
*/