-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathroman_to_integer.rs
More file actions
36 lines (35 loc) · 823 Bytes
/
roman_to_integer.rs
File metadata and controls
36 lines (35 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn rom_to_int(s: String) -> u32 {
let table: Vec<(u32, &'static str)> = vec![
(1000, "M"),
(900, "CM"),
(500, "D"),
(400, "CD"),
(100, "C"),
(90, "XC"),
(50, "L"),
(40, "XL"),
(10, "X"),
(9, "IX"),
(5, "V"),
(4, "IV"),
(1, "I"),
];
let mut sum = 0;
let mut i = 0;
for t in table.iter() {
while i + t.1.len() <= s.len() && t.1 == &s[i..i + t.1.len()] {
i += t.1.len();
sum += t.0;
if i >= s.len() {
return sum;
}
}
}
sum
}
#[test]
fn it_works() {
assert_eq!(rom_to_int("LVIII".to_string()), 58);
assert_eq!(rom_to_int("DCXXI".to_string()), 621);
assert_eq!(rom_to_int("MDXIV".to_string()), 1514);
}