Skip to content

Commit c8e0f90

Browse files
committed
✨ (roman-numerals): ConvertToArabic rest test case and refactor
1 parent d3a4fe4 commit c8e0f90

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

property-base-test/roman_number.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ func ConvertToRoman(arabic int) string {
3535
}
3636

3737
func ConvertToArabic(roman string) int {
38-
total := 0
39-
for range roman {
40-
total++
38+
var arabic = 0
39+
40+
for _, numeral := range allRomanNumerals {
41+
for strings.HasPrefix(roman, numeral.Symbol) {
42+
arabic += numeral.Value
43+
roman = strings.TrimPrefix(roman, numeral.Symbol)
44+
}
4145
}
42-
return total
46+
47+
return arabic
4348
}

property-base-test/roman_number_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestRomanNumerals(t *testing.T) {
5353
}
5454

5555
func TestConvertingToArabic(t *testing.T) {
56-
for _, test := range cases[:3] {
56+
for _, test := range cases {
5757
t.Run(fmt.Sprintf("%q gets converted to %d", test.Roman, test.Arabic), func(t *testing.T) {
5858
got := ConvertToArabic(test.Roman)
5959
if got != test.Arabic {

0 commit comments

Comments
 (0)