Back to Practice
#0223

Roman Numeral to Integer

MediumPython25 min20 XP

Problem

Convert a Roman numeral string to an integer. Handle subtractive cases like IV, IX, XL, and CM.

Why This Matters

Roman numeral parsing is a compact test of scanning rules and lookahead.

Function Signature

def roman_to_int(s):

Examples

Example 1
Inputs = "MCMXCIV"
Output1994

M = 1000, CM = 900, XC = 90, and IV = 4.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Prefer readable Python before micro-optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputs = "MCMXCIV"
Expected1994

M = 1000, CM = 900, XC = 90, and IV = 4.

Hidden Test Categories
single symbolno subtractive pairsmultiple subtractive pairs