Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing result with 1 instead of 0
Using an empty string instead of a number
✗ Incorrect
We start counting the integer value from zero before processing the Roman numeral string.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of s[i]
Trying to use roman_map[i] which is invalid
✗ Incorrect
We use s[i] to get the character at position i in the string s to look up its integer value.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<'
Using '==' or '!=' which are incorrect here
✗ Incorrect
If the current value is less than the next value, we subtract it according to Roman numeral rules.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 's' as loop variable instead of 'i'
Returning 'value' or 's' instead of 'result'
✗ Incorrect
The loop variable is 'i' to index the string, and we return the final integer 'result'.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'k' for values or 'v' for keys
Not starting with '{' for dictionary comprehension
✗ Incorrect
We start the dictionary with '{', use 'v' for values, 'k' for keys, and close with '}' (already in code).