Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-266

Validate Roman Numeral

Problem

Return True if the string is a valid Roman numeral in the standard range 1 to 3999, and False otherwise.

Parse the numeral using the subtractive rule, then convert the parsed value back to Roman form and compare with the input; canonical round-tripping rejects repetitions like IIII and invalid pairs like IC. The empty string is invalid.

Hint

Round-tripping through the canonical Roman form catches every invalid numeral.

Test cases

case 1
in: ["III"]
expected: true
case 2
in: ["IV"]
expected: true
case 3
in: ["IIII"]
expected: false
case 4
in: ["MCMXCIV"]
expected: true
case 5
in: ["IC"]
expected: false
case 6
in: [""]
expected: false
Starter codePython
def valid_roman(s):
    # Your code here
    pass

6 test cases · run your code against them in the editor