Skip to content

Latest commit

 

History

History
23 lines (14 loc) · 805 Bytes

File metadata and controls

23 lines (14 loc) · 805 Bytes

Roman Numerals

Converts between Roman numerals and integers in both directions. See Roman numerals — Wikipedia.

Rules

Standard Roman numeral values: I=1, V=5, X=10, L=50, C=100, D=500, M=1000

Subtractive notation: when a smaller value precedes a larger one, it is subtracted — e.g. IV=4, IX=9, XL=40, XC=90, CD=400, CM=900.

Approach

Roman → Integer: Scan right to left. If the current value is less than the previous, subtract it; otherwise add it.

Integer → Roman: Greedily subtract the largest symbol value that fits, including subtractive pairs.

Run

python roman_numerals.py          # built-in demo
python roman_numerals.py XIV      # Roman to integer
python roman_numerals.py 1994     # Integer to Roman