0
0
DSA Pythonprogramming~20 mins

Roman to Integer Conversion in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Roman Numeral Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Roman to Integer Conversion Function
What is the output of the following code when converting the Roman numeral 'MCMXCIV' to an integer?
DSA Python
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'))
A1904
BNone (KeyError)
C2094
D1994
Attempts:
2 left
💡 Hint
Remember that when a smaller value appears before a larger value, it should be subtracted.
🧠 Conceptual
intermediate
1:30remaining
Understanding Subtraction Rule in Roman Numerals
Which of the following Roman numeral pairs correctly demonstrates the subtraction rule used in Roman to Integer conversion?
AMM (2000), CC (200)
BVI (6), XI (11)
CIV (4), IX (9)
DXV (15), LX (60)
Attempts:
2 left
💡 Hint
Look for pairs where a smaller numeral comes before a larger numeral.
🔧 Debug
advanced
2:00remaining
Identify the Error in Roman to Integer Code
What error will the following code produce when run with input 'XLII'?
DSA Python
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 s:
        value = roman_map[char]
        if value > prev_value:
            total += value - 2 * prev_value
        else:
            total += value
        prev_value = value
    return total

print(roman_to_int('XLII'))
AKeyError
B42
CIncorrect output: 52
DSyntaxError
Attempts:
2 left
💡 Hint
Check how subtraction is handled when the current value is greater than the previous.
Predict Output
advanced
2:00remaining
Output of Incorrect Roman to Integer Implementation
What is the output of this code when converting 'IX' to an integer?
DSA Python
def roman_to_int(s):
    roman_map = {'I': 1, 'V': 5, 'X': 10}
    total = 0
    for i in range(len(s)):
        value = roman_map[s[i]]
        if i + 1 < len(s) and roman_map[s[i+1]] > value:
            total -= value
        else:
            total += value
    return total

print(roman_to_int('IX'))
A9
B11
C1
DKeyError
Attempts:
2 left
💡 Hint
Check how the code subtracts the value when the next numeral is larger.
🚀 Application
expert
3:00remaining
Number of Valid Roman Numerals of Length 3
How many valid Roman numerals of length exactly 3 characters exist using the symbols I, V, X, L, C, D, M following standard Roman numeral rules?
A64
B100
C49
D75
Attempts:
2 left
💡 Hint
Consider valid combinations and subtraction rules for 3-character numerals.