24Roman Numerals to Integer

    Mediumdictionariesstrings

    Write roman_to_int(s) that converts a Roman numeral to an integer.

    The symbols are I=1, V=5, X=10, L=50, C=100, D=500, M=1000. Normally they add up, but a smaller symbol before a larger one is subtracted: IV is 4 and XC is 90.

    Examples

    Example 1
    >>> roman_to_int('III')
    3
    Example 2
    >>> roman_to_int('MCMXCIV')
    1994

    Run examples checks the 2 examples above. Submit runs all 7 tests, including hidden edge cases, and marks the challenge solved.

    solution.py
    Loading Python…

    Test results show up here

    Write roman_to_int in the editor, then run the examples. Use print() freely: its output is shown for each test.