InferenceAI

Enter at least two characters to search.

Start free

Roman Numeral to Number

easyArrays & Strings15 min10 XP

Convert a valid Roman numeral (symbols I V X L C D M = 1, 5, 10, 50, 100, 500, 1000) to an integer. A smaller symbol before a larger one is subtracted, as in IV = 4 and CM = 900.

Constraints

  • The input represents a number from 1 to 3999.

Examples

EXAMPLE WALKTHROUGHadditive

A guided view of the published example, not a trace of your code.

INPUT["III"]
TASKroman_to_number()
EXPECTED OUTPUT3

Start with the input values shown in this example.

Example 1 of 3

Input: s = "III"

Output: 3

Explanation: additive

Input: s = "LVIII"

Output: 58

Explanation: mixed

Input: s = "MCMXCIV"

Output: 1994

Explanation: several subtractions

Hint 1

Scan left to right; subtract a value when the next symbol is larger, otherwise add it.

Topics

DSAArrays & StringsPython