How do you handle subtractive notation in Roman numerals (e.g., IV, IX)?
In subtractive notation, a smaller numeral before a larger numeral means you subtract the smaller from the larger (e.g., IV = 5 - 1 = 4).
Click to reveal answer
intermediate
What is the main approach to convert a Roman numeral string to an integer?
Traverse the string from left to right, add the value of each symbol, but if a smaller value precedes a larger one, subtract the smaller value instead of adding.
Click to reveal answer
beginner
Why do we subtract the value when a smaller Roman numeral comes before a larger one?
Because Roman numerals use subtractive notation to represent numbers like 4 (IV) or 9 (IX), where the smaller numeral before a larger one means subtraction.
Click to reveal answer
beginner
What data structure is commonly used to map Roman numerals to their integer values in code?
A dictionary (or map) is used to store Roman numeral characters as keys and their integer values as values for quick lookup.
Click to reveal answer
What integer does the Roman numeral 'IX' represent?
A9
B11
C6
D4
✗ Incorrect
'IX' means 10 - 1 = 9 using subtractive notation.
In the Roman numeral 'XIV', what operation is done when processing 'IV'?
ASubtract 1 from 5
BIgnore 'IV'
CMultiply 1 and 5
DAdd 1 and 5
✗ Incorrect
'IV' means 5 - 1 = 4 using subtractive notation.
Which data structure is best to store Roman numeral values for conversion?
AList
BDictionary
CStack
DQueue
✗ Incorrect
A dictionary allows quick lookup of Roman numeral values by their character keys.
When converting 'MCMXCIV', what is the integer result?
A1949
B2004
C1894
D1994
✗ Incorrect
M=1000, CM=900, XC=90, IV=4; sum is 1000+900+90+4=1994.
What should you do if a smaller Roman numeral appears after a larger one?
AIgnore the smaller numeral
BSubtract the smaller numeral
CAdd the smaller numeral
DMultiply the numerals
✗ Incorrect
If a smaller numeral comes after a larger one, you add its value.
Explain the step-by-step process to convert a Roman numeral string to an integer.
Think about how to handle subtractive notation while adding values.
You got /6 concepts.
Describe how subtractive notation works in Roman numerals and why it is important for conversion.
Focus on the position of smaller numerals relative to larger ones.
You got /4 concepts.
Practice
(1/5)
1. What is the integer value of the Roman numeral IX?
easy
A. 11
B. 19
C. 6
D. 9
Solution
Step 1: Understand Roman numeral rules for subtraction
In Roman numerals, when a smaller numeral appears before a larger one, it is subtracted. Here, I (1) is before X (10), so 1 is subtracted from 10.
Step 2: Calculate the value of IX
10 - 1 = 9.
Final Answer:
9 -> Option D
Quick Check:
IX = 9 [OK]
Hint: Subtract when smaller numeral is before larger one [OK]
Common Mistakes:
Adding values instead of subtracting
Confusing IX with XI
Ignoring subtraction rule
2. Which of the following Python code snippets correctly initializes a dictionary mapping Roman numerals to integers?
Hint: Use curly braces and quote keys for dictionaries [OK]
Common Mistakes:
Using square brackets instead of curly braces
Not quoting string keys
Using assignment '=' inside dictionary
3. What is the output of the following Python code?
def roman_to_int(s):
roman_map = {'I': 1, 'V': 5, 'X': 10}
total = 0
for i in range(len(s)):
if i + 1 < len(s) and roman_map[s[i]] < roman_map[s[i + 1]]:
total -= roman_map[s[i]]
else:
total += roman_map[s[i]]
return total
print(roman_to_int('XIV'))
medium
A. 14
B. 16
C. 9
D. 11
Solution
Step 1: Trace the loop for input 'XIV'
Characters: X(10), I(1), V(5). Loop index i from 0 to 2. i=0: X(10) < I(1)? No, add 10. i=1: I(1) < V(5)? Yes, subtract 1. i=2: V(5), no next char, add 5.
Step 2: Calculate total
Total = 10 - 1 + 5 = 14.
Final Answer:
14 -> Option A
Quick Check:
X(10) + IV(4) = 14 [OK]
Hint: Subtract if current < next numeral, else add [OK]
Common Mistakes:
Adding all values without subtraction
Off-by-one errors in loop
Ignoring last character addition
4. The following code is intended to convert a Roman numeral string to an integer. What is the error?
def roman_to_int(s):
roman_map = {'I': 1, 'V': 5, 'X': 10}
total = 0
for i in range(len(s)):
if roman_map[s[i]] < roman_map[s[i + 1]]:
total -= roman_map[s[i]]
else:
total += roman_map[s[i]]
return total
print(roman_to_int('IX'))
medium
A. SyntaxError due to missing colon
B. KeyError because 'IX' is not in roman_map
C. IndexError due to accessing s[i + 1] without boundary check
D. Returns incorrect value 11 instead of 9
Solution
Step 1: Identify loop and index usage
The loop runs from i=0 to len(s)-1. At last index i, accessing s[i+1] causes IndexError.
Step 2: Explain why error occurs
For input 'IX' (length 2), when i=1, s[i+1] is s[2], which is out of range.
Final Answer:
IndexError due to accessing s[i + 1] without boundary check -> Option C
Quick Check:
Check index bounds before s[i+1] access [OK]
Hint: Always check index before accessing s[i+1] [OK]
Common Mistakes:
Not checking index bounds in loops
Assuming s[i+1] always exists
Ignoring runtime errors
5. Given the Roman numeral string 'MCMXCIV', what integer does the following code return?
def roman_to_int(s):
roman_map = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
total = 0
prev_value = 0
for char in reversed(s):
value = roman_map[char]
if value < prev_value:
total -= value
else:
total += value
prev_value = value
return total
print(roman_to_int('MCMXCIV'))
hard
A. 1494
B. 1994
C. 1949
D. 2094
Solution
Step 1: Understand reversed traversal and subtraction logic
The code processes characters from right to left, subtracting if current value is less than previous.