roman: add 5 and refactor

This commit is contained in:
vinchent 2024-09-22 21:02:27 +02:00
parent 2515644d4f
commit 0a75d86850
2 changed files with 12 additions and 4 deletions

View File

@ -4,12 +4,19 @@ import "strings"
func ConvertToRoman(arabic int) string {
var converted strings.Builder
for i := arabic; i > 0; i-- {
if i == 4 {
for arabic > 0 {
switch {
case arabic > 4:
converted.WriteString("V")
arabic -= 5
case arabic > 3:
converted.WriteString("IV")
break
}
arabic -= 4
default:
converted.WriteString("I")
arabic -= 1
}
}
return converted.String()
}

View File

@ -12,6 +12,7 @@ func TestRomanNemerals(t *testing.T) {
{"2 gets converted to II", 2, "II"},
{"3 gets converted to III", 3, "III"},
{"4 gets converted to IV", 4, "IV"},
{"5 gets converted to V", 5, "V"},
}
for _, test := range cases {
t.Run(test.Description, func(t *testing.T) {