0
0
DSA Pythonprogramming~10 mins

Roman to Integer Conversion in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the result variable to zero.

DSA Python
def roman_to_int(s):
    result = [1]
    return result
Drag options to blanks, or click blank then click option'
A0
B1
CNone
D''
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing result with 1 instead of 0
Using an empty string instead of a number
2fill in blank
medium

Complete the code to get the integer value for the current Roman numeral character.

DSA Python
def roman_to_int(s):
    roman_map = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
    result = 0
    for i in range(len(s)):
        value = roman_map[[1]]
    return result
Drag options to blanks, or click blank then click option'
Ai
Bs[i]
Cs
Droman_map[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of s[i]
Trying to use roman_map[i] which is invalid
3fill in blank
hard

Fix the error in the condition to check if the current value is less than the next value.

DSA Python
def roman_to_int(s):
    roman_map = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
    result = 0
    for i in range(len(s)):
        value = roman_map[s[i]]
        if i + 1 < len(s) and value [1] roman_map[s[i + 1]]:
            result -= value
        else:
            result += value
    return result
Drag options to blanks, or click blank then click option'
A>
B!=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<'
Using '==' or '!=' which are incorrect here
4fill in blank
hard

Fill both blanks to complete the loop and return statement correctly.

DSA Python
def roman_to_int(s):
    roman_map = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
    result = 0
    for [1] in range(len(s)):
        value = roman_map[s[i]]
        if i + 1 < len(s) and value < roman_map[s[i + 1]]:
            result -= value
        else:
            result += value
    return [2]
Drag options to blanks, or click blank then click option'
Ai
Bs
Cresult
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 's' as loop variable instead of 'i'
Returning 'value' or 's' instead of 'result'
5fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps Roman numerals to their integer values for given keys and values.

DSA Python
keys = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
values = [1, 5, 10, 50, 100, 500, 1000]
roman_map = { [2] : [1] for [3] in zip(keys, values)}
Drag options to blanks, or click blank then click option'
A{
Bv
Ck
D}
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'k' for values or 'v' for keys
Not starting with '{' for dictionary comprehension