Skip to content

Commit 9986d1e

Browse files
committed
✨ (roman-numerals): add sixth-test and refactor
1 parent b867486 commit 9986d1e

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

property-base-test/roman_number.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@ package roman_numerals
22

33
import "strings"
44

5+
type RomanNumerals struct {
6+
Value int
7+
Symbol string
8+
}
9+
10+
var allRomanNumerals = []RomanNumerals{
11+
{10, "X"},
12+
{9, "IX"},
13+
{5, "V"},
14+
{4, "IV"},
15+
{1, "I"},
16+
}
17+
518
func ConvertToRoman(arabic int) string {
619
var result strings.Builder
7-
for arabic > 0 {
8-
switch {
9-
case arabic > 4:
10-
arabic -= 5
11-
result.WriteString("V")
12-
case arabic > 3:
13-
arabic -= 4
14-
result.WriteString("IV")
15-
default:
16-
arabic--
17-
result.WriteString("I")
20+
for _, romanNumeral := range allRomanNumerals {
21+
if arabic >= romanNumeral.Value {
22+
arabic -= romanNumeral.Value
23+
result.WriteString(romanNumeral.Symbol)
1824
}
1925
}
2026
return result.String()

property-base-test/roman_number_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func TestRomanNumerals(t *testing.T) {
1313
{"3 gets converted to III", 3, "III"},
1414
{"4 gets converted to IV", 4, "IV"},
1515
{"5 gets converted to V", 5, "V"},
16+
{"9 gets converted to IX", 9, "IX"},
1617
}
1718

1819
for _, test := range cases {

0 commit comments

Comments
 (0)