Decode an Ancient Numeral String into a Number
StringsAuto-graded · 24 test casesLast updated
Parse a valid Roman numeral string into its integer value by handling additive symbols and the six subtractive pairs.
Decode an Ancient Numeral String into a Number
A scribe records whole numbers using seven letters, each worth a fixed amount:
| Letter | Worth |
|---|---|
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Letters are normally written from the largest worth to the smallest, reading left to right, and the worths are added together. There is one exception: to express certain values, a smaller letter is written immediately before a larger one, and in that case the smaller letter's worth is subtracted instead of added. Exactly six such combinations exist:
IbeforeVgives 4, andIbeforeXgives 9.XbeforeLgives 40, andXbeforeCgives 90.CbeforeDgives 400, andCbeforeMgives 900.
For example, 4 is written IV (not IIII), and 1994 is written MCMXCIV, which reads as M + CM + XC + IV = 1000 + 900 + 90 + 4.
Task
Given a string numeral containing a valid numeral written this way, return the integer it represents.
Constraints
1 <= len(numeral) <= 15numeralconsists only of the charactersI,V,X,L,C,D,M.numeralis guaranteed to be a valid numeral whose value lies in the range[1, 3999].- The function returns a single integer.
Examples
Example 1
Input: decode_numeral('III')
Output: 3
Example 2
Input: decode_numeral('MCMXCIV')
Output: 1994
Example 3
Input: decode_numeral('LVIII')
Output: 58
Example 4
Input: decode_numeral('IX')
Output: 9
Hints
Hint 1
Start with a lookup table that maps each letter to its worth, so you can convert characters to numbers in constant time.
Hint 2
A subtractive pair such as CM equals M minus C. Instead of recognizing pairs as units, decide for each letter on its own whether it is added or subtracted.
Hint 3
Compare each letter's worth with the worth of the letter right after it: if it is strictly smaller, subtract it, otherwise add it. Take care at the last character, which has no successor.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Almost everything in the numeral is additive: you add up the worth of each letter. The only twist is the six subtractive pairs, where a smaller letter stands directly before a larger one.
A pair like CM is worth M - C, which equals -C + M. So there is no need to detect pairs as units. Process each letter independently with one rule:
- If its worth is strictly less than the worth of the next letter, subtract it.
- Otherwise, add it.
The last letter has no successor, so it is always added.
For MCMXCIV: +1000 (M), -100 (C, next is M), +1000 (M), -10 (X, next is C), +100 (C), -1 (I, next is V), +5 (V) gives 1994.
Why it is correct
In a valid numeral, worths never increase from left to right except at the first letter of a subtractive pair. So "current worth < next worth" is true exactly when the current letter is the left half of a subtractive pair.
- A letter not in a subtractive pair is at least as large as its successor, so it is added, which is right.
- For a pair
abwithworth(a) < worth(b),ais subtracted. Thenbis compared with its own successor, which in a valid numeral is always smaller, sobis added. The net contribution isworth(b) - worth(a), the pair's value.
Summing every contribution gives the value of the numeral.
Complexity
- Time: O(n) for a string of length n, with one dictionary lookup and one comparison per character.
- Space: O(1), since the lookup table has a fixed seven entries and only a running total is kept.
Reference solution
def decode_numeral(numeral):
worth = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
total = 0
n = len(numeral)
for i, ch in enumerate(numeral):
value = worth[ch]
if i + 1 < n and value < worth[numeral[i + 1]]:
total -= value
else:
total += value
return total