📖 Scenario: You are building a simple tool to convert Roman numerals into regular numbers. Roman numerals are used in clocks, book chapters, and movie titles. Your program will help translate these into numbers we use every day.
🎯 Goal: Create a Go program that converts a Roman numeral string into its integer value using a map and a loop.
📋 What You'll Learn
Create a map with Roman numeral characters as keys and their integer values as values.
Create a variable to hold the total integer value.
Use a for loop to iterate over the Roman numeral string and apply the conversion logic.
Print the final integer value.
💡 Why This Matters
🌍 Real World
Roman numerals appear in clocks, book chapters, movie titles, and historical dates. Converting them to integers helps computers understand and process these values.
💼 Career
Understanding how to map characters to values and process strings with loops and conditions is fundamental in programming jobs, especially in data parsing and conversion tasks.
Progress0 / 4 steps
1
Create the Roman numeral map
Create a map called romanMap with these exact entries: 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000.
DSA Go
Hint
Use map[rune]int to create the map with rune keys and int values.
2
Create the input Roman numeral and total variable
Create a string variable called roman and set it to "MCMXCIV". Create an integer variable called total and set it to 0.
DSA Go
Hint
Use roman := "MCMXCIV" and total := 0.
3
Convert Roman numeral to integer
Use a for loop with index i from 0 to len(roman)-1. Inside the loop, get the value of the current Roman character from romanMap. If i + 1 is less than len(roman) and the value of the current character is less than the value of the next character, subtract the current value from total. Otherwise, add the current value to total.
DSA Go
Hint
Use for i := 0; i < len(roman); i++ and check if current value is less than next value to decide addition or subtraction.
4
Print the integer result
Print the value of total using fmt.Println(total).
DSA Go
Hint
Use fmt.Println(total) to display the final integer.
Practice
(1/5)
1. What is the value of the Roman numeral IX when converted to an integer?
easy
A. 6
B. 11
C. 19
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 integer value
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 Go code snippets correctly initializes a map for Roman numeral values?
easy
A. romanMap := map[string]int{"I" = 1, "V" = 5, "X" = 10}
B. romanMap := map[int]string{1: "I", 5: "V", 10: "X"}
C. romanMap := map[string]int{"I": 1, "V": 5, "X": 10}
D. romanMap := map[string]int{I: 1, V: 5, X: 10}
Solution
Step 1: Check map key and value types
The map should have string keys (Roman numerals) and int values (their integer equivalents). romanMap := map[string]int{"I": 1, "V": 5, "X": 10} uses map[string]int correctly.
Step 2: Verify syntax correctness
romanMap := map[string]int{"I": 1, "V": 5, "X": 10} uses correct Go syntax with colon ':' to assign values. Options A and C have syntax errors, and B reverses key-value types.
Correct map syntax uses colons and proper types [OK]
Hint: Use colon ':' for map key-value pairs in Go [OK]
Common Mistakes:
Using '=' instead of ':' in map initialization
Swapping key and value types
Omitting quotes around string keys
3. What will be the output of the following Go code snippet?
func romanToInt(s string) int {
romanMap := map[byte]int{'I': 1, 'V': 5, 'X': 10}
total := 0
for i := 0; i < len(s); i++ {
if i+1 < len(s) && romanMap[s[i]] < romanMap[s[i+1]] {
total -= romanMap[s[i]]
} else {
total += romanMap[s[i]]
}
}
return total
}
fmt.Println(romanToInt("IV"))
medium
A. 6
B. 4
C. 9
D. Error
Solution
Step 1: Analyze the loop and subtraction logic
The code subtracts the value if the current numeral is less than the next one, else adds it. For "IV", 'I' (1) < 'V' (5), so subtract 1, then add 5.
Step 2: Calculate total value
Total = -1 + 5 = 4.
Final Answer:
4 -> Option B
Quick Check:
IV = 4 using subtraction rule [OK]
Hint: Subtract when current < next numeral [OK]
Common Mistakes:
Adding all values without subtraction
Index out of range errors
Confusing byte and string keys in map
4. The following Go function is intended to convert Roman numerals to integers. What is the bug?
func romanToInt(s string) int {
romanMap := map[byte]int{'I': 1, 'V': 5, 'X': 10}
total := 0
for i := 0; i < len(s); i++ {
if romanMap[s[i]] < romanMap[s[i+1]] {
total -= romanMap[s[i]]
} else {
total += romanMap[s[i]]
}
}
return total
}
medium
A. Missing check for i+1 < len(s) causing index out of range
B. Using map with byte keys instead of string keys
C. Incorrect subtraction logic for Roman numerals
D. Returning total before loop ends
Solution
Step 1: Identify loop boundary issue
The code accesses s[i+1] without checking if i+1 is within string length, causing possible index out of range error.