21. Roman to integer
Roman numerals use I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000. Values are normally written largest first and added up, but a smaller value placed directly before a larger one is subtracted: IV is 4, XC is 90 and CM is 900.
Write romanToInt s, the value of the numeral s.
Examples
- Input
romanToInt "III"Output3 - Input
romanToInt "LVIII"Output58 - Input
romanToInt "MCMXCIV"Output1994
Submitting also runs 8 hidden tests.
Constraints
- Inputs are valid numerals from 1 to 3999.
Loading editor…
Type
\to in the editor, or click:Run checks your code against the examples and your custom inputs and records nothing. Submit also runs the hidden tests, confirms the axioms your proof uses and records the result. Each uses one compiler check.
ReadyLn 1, Col 1Lean 4
Draft saved in this browser