Your question is Roman Numeral Conversion Method. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Global Healthcare Exchange systems may receive compact Roman numeral identifiers from legacy healthcare documents. Implement a parser that converts a Roman numeral to its integer value only when the numeral follows canonical Roman numeral rules.
Return the decoded integer for a valid numeral. Return -1 for an invalid numeral.
Implement roman_to_integer(s), where s is a non-empty uppercase string containing only the characters I, V, X, L, C, D, and M. Valid values range from 1 through 3999 and must use canonical notation:
I, X, and C may precede only their valid subtractive partners: IV, IX, XL, XC, CD, or CM.V, L, and D may not repeat.The function returns an integer for valid input and -1 otherwise. Aim for a single left-to-right parse and explain how canonical validation prevents malformed strings from being accepted.
def roman_to_integer(s):