***roman: add convert to arabic, the real one

This commit is contained in:
vinchent 2024-09-22 21:36:03 +02:00
parent 715ea6477a
commit 52825e49b3
2 changed files with 10 additions and 2 deletions

View File

@ -37,5 +37,13 @@ func ConvertToRoman(arabic int) string {
}
func ConvertToArabic(roman string) int {
return 1
var converted int
for _, numeral := range allRomanNumerals {
for strings.HasPrefix(roman, numeral.Symbol) {
converted += numeral.Value
// roman = roman[len(numeral.Symbol):]
roman = strings.TrimPrefix(roman, numeral.Symbol)
}
}
return converted
}

View File

@ -54,7 +54,7 @@ func TestRomanNemerals(t *testing.T) {
}
func TestConvertingToArabic(t *testing.T) {
for _, test := range cases[:1] {
for _, test := range cases {
t.Run(fmt.Sprintf("%q gets converted to %d", test.Roman, test.Arabic), func(t *testing.T) {
got := ConvertToArabic(test.Roman)
want := test.Arabic