Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the integer value of a Roman numeral character.
DSA C#
int GetValue(char roman) {
switch (roman) {
case 'I': return [1];
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of 1 for 'I'.
Confusing 'I' with 'V' or 'X'.
✗ Incorrect
The Roman numeral 'I' represents the integer 1.
2fill in blank
mediumComplete the code to iterate through the Roman numeral string from left to right.
DSA C#
for (int i = 0; i [1] s.Length; i++) { // process s[i] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causing index out of range exception.
Using '>' or '>=' which never enter the loop.
✗ Incorrect
The loop should run while i is less than s.Length to avoid out-of-range errors.
3fill in blank
hardFix the error in the condition to check if the current Roman numeral is less than the next one.
DSA C#
if (GetValue(s[i]) [1] GetValue(s[i + 1])) { result -= GetValue(s[i]); } else { result += GetValue(s[i]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which reverses logic.
Using '==' which misses subtraction cases.
✗ Incorrect
If the current value is less than the next, subtract it according to Roman numeral rules.
4fill in blank
hardFill both blanks to correctly handle the last character in the Roman numeral string.
DSA C#
if (i == [1] - 1) { result += GetValue(s[[2]]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using s.Length which is out of range.
Using 0 which is the first character, not last.
✗ Incorrect
The last index is s.Length - 1, so add the value of s at that index.
5fill in blank
hardFill all three blanks to complete the dictionary comprehension that maps Roman numerals to integers.
DSA C#
var romanMap = new Dictionary<char, int> {
{'I', [1],
{'V', [2],
{'X', [3]
};Attempts:
3 left
💡 Hint
Common Mistakes
Mixing values like assigning 10 to 'V'.
Using 50 for 'X' which is incorrect.
✗ Incorrect
Roman numerals 'I', 'V', and 'X' correspond to 1, 5, and 10 respectively.
