Roman Numerals

Level: Beginner

Concepts: Algorithms Numbers


Create a Roman numeral converter that can convert between Roman numerals and Arabic numbers.

Requirements

  1. Convert Arabic numbers to Roman numerals (1 to 3999)
  2. Convert Roman numerals to Arabic numbers
  3. Handle invalid Roman numerals appropriately
  4. Follow standard Roman numeral rules:
    • I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000
    • Letters can be repeated up to 3 times (e.g., III = 3)
    • When a smaller value precedes a larger value, subtract the smaller (e.g., IV = 4)
    • Only I, X, and C can be used as subtractive numerals

Test Cases

Arabic Number Roman Numeral Notes
1 I Basic conversion
4 IV Subtractive notation
9 IX Subtractive notation
40 XL Subtractive notation
90 XC Subtractive notation
400 CD Subtractive notation
900 CM Subtractive notation
1984 MCMLXXXIV Complex number
3999 MMMCMXCIX Maximum valid number

Invalid Cases to Handle

  • Numbers less than 1 or greater than 3999
  • Invalid Roman numeral characters
  • Invalid Roman numeral sequences (e.g., "IIII", "VV")
  • Invalid subtractive combinations (e.g., "IL", "IC")

Tips

  • Start with the simplest conversions first (1-10)
  • Add support for subtractive notation gradually
  • Consider edge cases and invalid inputs
  • Think about how to validate Roman numerals
  • Consider using a state machine for validation