Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 216
Good morning! Here's your coding interview problem for today.

This problem was asked by Facebook.

Given a number in Roman numeral format, convert it to decimal.

The values of Roman numerals are as follows:
```
{
    'M': 1000,
    'D': 500,
    'C': 100,
    'L': 50,
    'X': 10,
    'V': 5,
    'I': 1
}
```
In addition, note that the Roman numeral system uses subtractive notation for numbers such as IV and XL.

For the input XIV, for instance, you should return 14.

Pull request description:
Day216
Resolves #445.

Patch:
diff --git a/README.md b/README.md
index 7a38b7b..b9f072f 100644
--- a/README.md
+++ b/README.md
@@ -226,3 +226,4 @@ problems from
 * [Day 213](https://github.com/vaskoz/dailycodingproblem-go/issues/439)
 * [Day 214](https://github.com/vaskoz/dailycodingproblem-go/issues/441)
 * [Day 215](https://github.com/vaskoz/dailycodingproblem-go/issues/443)
+* [Day 216](https://github.com/vaskoz/dailycodingproblem-go/issues/445)
diff --git a/day216/problem.go b/day216/problem.go
new file mode 100644
index 0000000..427fa62
--- /dev/null
+++ b/day216/problem.go
@@ -0,0 +1,32 @@
+package day216
+
+var romanNumeralMap = map[rune]int{
+	'M': 1000,
+	'D': 500,
+	'C': 100,
+	'L': 50,
+	'X': 10,
+	'V': 5,
+	'I': 1,
+}
+
+// ConvertRomanNumeralToInteger converts and roman numeral string
+// into an integer.
+// Only works on non-negative integers (no decimals).
+func ConvertRomanNumeralToInteger(roman string) int {
+	var result int
+	for i := 0; i < len(roman); i++ {
+		if first := romanNumeralMap[rune(roman[i])]; i+1 < len(roman) {
+			if second := romanNumeralMap[rune(roman[i+1])]; first >= second {
+				result += first
+			} else {
+				result += second - first
+				i++
+			}
+		} else {
+			result += first
+			i++
+		}
+	}
+	return result
+}


Test patch:
diff --git a/day216/problem_test.go b/day216/problem_test.go
new file mode 100644
index 0000000..89f3ade
--- /dev/null
+++ b/day216/problem_test.go
@@ -0,0 +1,32 @@
+package day216
+
+import "testing"
+
+var testcases = []struct {
+	roman    string
+	expected int
+}{
+	{"IV", 4},
+	{"XL", 40},
+	{"XIV", 14},
+	{"MMXIX", 2019},
+	{"XVI", 16},
+	{"XMMMM", 3990},
+}
+
+func TestConvertRomanNumeralToInteger(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := ConvertRomanNumeralToInteger(tc.roman); result != tc.expected {
+			t.Errorf("Expected %v, got %v", tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkConvertRomanNumeralToInteger(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			ConvertRomanNumeralToInteger(tc.roman)
+		}
+	}
+}