Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-049

Roman to Integer

Problem

Convert a valid Roman numeral string to its integer value.

Add each symbol's value, except when a smaller symbol precedes a larger one, in which case subtract the smaller value, as in IV = 4 and IX = 9. Assume s is a valid numeral between 1 and 3999.

Hint

Only the subtractive pairs put a smaller symbol before a larger one.

Test cases

case 1
in: ["III"]
expected: 3
case 2
in: ["IV"]
expected: 4
case 3
in: ["MCMXCIV"]
expected: 1994
case 4
in: ["LVIII"]
expected: 58
case 5
in: ["IX"]
expected: 9
Starter codePython
def roman_to_int(s):
    # Your code here
    pass

5 test cases · run your code against them in the editor