progress

~/problems / Math & matrices

Roman to Integer

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or fight a boss.

easy ~10 min

A museum is cataloguing dates carved in Roman numerals:

Symbol I V X L C D M
Value 1 5 10 50 100 500 1000

Symbols go largest first and add up (XVII = 17), except six subtracting pairs: IV = 4, IX = 9, XL = 40, XC = 90, CD = 400, CM = 900.

Write roman_value(s) -> int for a valid numeral from 1 to 3999.

roman_value("XVII")        # 17
roman_value("XLIX")        # 49
roman_value("MCMXCIV")     # 1994
roman_value("MMMCMXCIX")   # 3999
Show hint

compare each symbol with the one after it. What does a smaller left-hand symbol mean?

Topic: Math and matrices. Digit-by-digit arithmetic, matrix rotation and in-place tricks.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc