Editor

Roman Numerals to Integer

#65 · Strings · Intermediate · 60 XP · about 9 min

Each Roman symbol has a value: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000. Normally the values are added from left to right, but a symbol that is followed by a larger one is subtracted instead: the I in IX makes it 9, and the C in CM makes it 900.

Write from_roman(numeral) that returns the value of a Roman numeral as an integer.

Examples

from_roman("MCMXCIV")  # returns 1994
from_roman("XLII")     # returns 42
from_roman("III")      # returns 3

MCMXCIV is M (1000) + CM (900) + XC (90) + IV (4) = 1994.

Constraints

  • numeral is a valid uppercase Roman numeral for a value from 1 to 3999.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.