use std::collections::HashMap;
fn roman_to_int(s: &str) -> i32 {
let roman_map: HashMap<char, i32> = [
('I', 1),
('V', 5),
('X', 10),
('L', 50),
('C', 100),
('D', 500),
('M', 1000),
]
.iter()
.cloned()
.collect();
let mut total = 0;
let mut prev_value = 0;
// iterate backwards over the string
for ch in s.chars().rev() {
let current_value = roman_map[&ch];
if current_value < prev_value {
total -= current_value;
} else {
total += current_value;
}
prev_value = current_value;
}
total
}
fn main() {
let roman = "XCVII";
let result = roman_to_int(roman);
println!("The integer value of {} is {}", roman, result);
}
/*
XCVII =
XC+V+I+I =
90+5+1+1 =
97
*/
/*
run:
The integer value of XCVII is 97
*/