Skip to content

Commit b867486

Browse files
committed
✨ (roman-numerals): add fith test and refactor
1 parent be4efb6 commit b867486

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

property-base-test/roman_number.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import "strings"
44

55
func ConvertToRoman(arabic int) string {
66
var result strings.Builder
7-
for i := arabic; i > 0; i-- {
8-
if arabic == 4 {
7+
for arabic > 0 {
8+
switch {
9+
case arabic > 4:
10+
arabic -= 5
11+
result.WriteString("V")
12+
case arabic > 3:
13+
arabic -= 4
914
result.WriteString("IV")
10-
break
15+
default:
16+
arabic--
17+
result.WriteString("I")
1118
}
12-
result.WriteString("I")
1319
}
1420
return result.String()
1521
}

property-base-test/roman_number_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func TestRomanNumerals(t *testing.T) {
1212
{"2 gets converted to II", 2, "II"},
1313
{"3 gets converted to III", 3, "III"},
1414
{"4 gets converted to IV", 4, "IV"},
15+
{"5 gets converted to V", 5, "V"},
1516
}
1617

1718
for _, test := range cases {

0 commit comments

Comments
 (0)